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!

Mail merge Word Document to Database

COM and Automation

Mail merge Word Document to Database

by  danceman  Posted    (Edited  )
This is a demo of using Word with Visual FoxPro to perform a Mail Merge. The Data attached is Free FoxPro table. In a program the table can be created from user selected criteria. The Word file is already setup to work with the set table. I use this with corporate letters that only need the fill in. FoxPro reports works well here but this does give some freedom to the user to modify the letter without programmer intervention.

Here is the FoxPro code

#DEFINE wdMainAndDataSource 2
#DEFINE wdSendToPrinter 1
#DEFINE wdDoNotSaveChanges 0
#DEFINE wdWindowStateMinimize 2

oWord = CREATEOBJECT("Word.Application")
oWord.Visible = .f.
oWord.WindowState = wdWindowStateMinimize

** change the path and file name to suit
oWord.Documents.Open("C:\wordmail.doc")

** check to make sure the documnet is attached to a
** datasource and ready to merge.
If oWord.ActiveDocument.MailMerge.State = wdMainAndDataSource
oWord.ActiveDocument.MailMerge.Destination = wdSendToPrinter
oWord.ActiveDocument.MailMerge.DataSource.FirstRecord = 1
oWord.ActiveDocument.MailMerge.DataSource.LastRecord = 1
oWord.ActiveDocument.MailMerge.Execute
endif
oWord.Quit(wdDoNotSaveChanges) && close file no save
release oWord

** property to retrive OR set the first record to print
*oWord.ActiveDocument.MailMerge.DataSource.FirstRecord

** property to retrive OR set the last record to print
*oWord.ActiveDocument.MailMerge.DataSource.LastRecord
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