Create the mail merge document in Microsoft Word. Start with a new
document in Microsoft Word. Format the Document as follows:
Contact
Company
Phone
Dear greeting,
<This is the body of the form letter.>
Sincerely,
<Your Company Name>
Create bookmarks for Contact, Company, Phone, and greeting. To do this,
select the item in the document (for example, Contact), choose the Edit
menu pad (it’s the View menu pad in Word 97), and click the Bookmark
menu item. Enter the bookmark name (for example, contact), and click
Add. The bookmark name should be the same as the item selected. Repeat
this for all four items. Save the Microsoft Word Document as Wordole.doc
in the Vfp\Samples\Data directory.
Create the OLE Automation program in Visual FoxPro. Create a new program
in Visual FoxPro with the following code:
NOTE: In Word 6.0 and Word 95, use the following code:
PUBLIC oWordObj
OPEN DATA SYS(2004)+"\samples\data\TestData.dbc"
USE Customer
oWordObj=CREATEOBJECT("Word.Basic")
oWordObj.FileOpen (SYS(2004)+"samples\data\wordole.doc")
oWordObj.EditGoto ("contact")
oWordObj.Insert (Customer.Contact)
oWordObj.EditGoTo ("company")
oWordObj.Insert (Customer.Company)
oWordObj.EditGoTo ("phone")
oWordObj.Insert (Customer.Phone)
oWordObj.EditGoTo ("greeting")
oWordObj.Insert (Customer.Contact)
oWordObj.FilePrint
NOTE: In Word 97, use the following code:
PUBLIC oWordObj
OPEN DATA SYS(2004)+"\samples\data\TestData.dbc"
USE Customer
oWordObj=CREATEOBJECT("Word.Basic")
oWordObj.FileOpen (SYS(2004)+"samples\data\wordole.doc")
oWordObj.ww7_EditGoto ("contact")
oWordObj.Insert (Customer.Contact)
oWordObj. ww7_EditGoTo ("company")
oWordObj.Insert (Customer.Company)
oWordObj. ww7_EditGoTo ("phone")
oWordObj.Insert (Customer.Phone)
oWordObj. ww7_EditGoTo ("greeting")
oWordObj.Insert (Customer.Contact)
oWordObj.FilePrint
Save and run the program. When the program is run, the form letter
should print using data from record one in the Customer table.
By removing the Open Data and Use Customer commands, you could call this
program from a form that was based on the customer table. If the program
is called from the click event of a command button, the form letter
would be printed using data from the current record in the customer
table.