Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I pass data to MS word document?

MS Word Automation

How do I pass data to MS word document?

by  tty0  Posted    (Edited  )
As this has been asked a couple of times over the past month I thought I would do a quick writeup and some links.

There are many uses for Word using VB6, one of them is as below:
I had a client that wished to build letters in MSWord which they could pick and choose at will and populate with customer names and addresses etc from a list. So each letter that they designed in word has a set of bookmarks, one for each of the customer details. The software then went off and according to a criteria built a list and dumped the letters to a printer with the correct details on.

To make this easier to see I have put only the code for the transfer from a form with 5 textboxes on it labelled

txtName
txtAddress
txtCode
txtPhone
txtFax

the code is behind a command button which takes the strings from the text boxes and puts them into the appropriate bookmarks.
'*********************************
Private Sub cmdButtom_Click()

Dim WordObj As Word.Application
Set WordObj = CreateObject("Word.Application")
Dim objWord As Word.Document
Set objWord = WordObj.Documents.Open(FileName:=App.Path & "\INVOICE.doc")
WordObj.visible = true
With objWord.Bookmarks
.Item("NAME").Range.Text = txtName
.Item("ADDRESS").Range.Text = txtAddress
.Item("CODE").Range.Text = txtCode
.Item("PHONE").Range.Text = txtPhone
.Item("FAX").Range.Text = txtFax
End With
Set objWord = Nothing


End Sub
'************************************

The word object model is extensive as are most and a full run down can be found on the MS site here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/modcore/html/deovrMicrosoftWord2000.asp

If you are wanting a demo of the code zipped up with the document, email me on chris@techsupportuk.com or ICQ 82621399 and i'll send it on.

Have fun!
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top