Windows Forms and Word Automation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vbnewbie1217
    New Member
    • Mar 2008
    • 4

    Windows Forms and Word Automation

    All,

    I would like to populate a word document using a windows form (i.e. have a user enter text into a textbox in windows form (i.e. textbox1) and then click submit and have the application open a word document that I have created and place the users text into a form field using a bookmark (i.e. bookmark1).

    I have done this in Access using VBA (i.e. using WordDocument.Fo rmFields("Bookm ark_Name").Resu lt = Forms("Form_Nam e").Field.Value ) but can't get this to translate into VB.NET. I have the form designed and am able to open the document, but can't get the text to populate.

    I am using VS Pro 2K5 and Office 2K3.

    Thanks in advance!

    Jeff
  • jg007
    Contributor
    • Mar 2008
    • 283

    #2
    Originally posted by vbnewbie1217
    All,

    I would like to populate a word document using a windows form (i.e. have a user enter text into a textbox in windows form (i.e. textbox1) and then click submit and have the application open a word document that I have created and place the users text into a form field using a bookmark (i.e. bookmark1).


    Jeff
    lol took me some time to work out but -


    this opens "c:\test.do c" , moves to the bookmark called ' bookmark_name ' and then pastes the contents of textbox1

    Code:
    Imports Microsoft.Office.Interop
    
    Public Class Form1
        Dim wordapp As Word.ApplicationClass
    
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            'open word document
            wordapp = New Word.Application
            Dim objdoc As Word.Document = wordapp.Documents.Open("c:\test")
    
            'go to bookmark
            wordapp.Selection.GoTo(What:=Word.WdGoToItem.wdGoToBookmark, Name:="Bookmark_Name")
    
            ' enter text from textbox
            wordapp.Selection.TypeText(TextBox1.Text)
    
            'make document visible
            wordapp.Visible = True
    
        End Sub
    
    End Class

    Comment

    • vbnewbie1217
      New Member
      • Mar 2008
      • 4

      #3
      YOU ROCK! It works...

      Comment

      Working...