Microsoft KB Archive/138205

= How to use OLE automation in Visual FoxPro with Microsoft Word =

Article ID: 138205

Article Last Modified on 2/24/2005

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0 Standard Edition
 * Microsoft Visual FoxPro 5.0 Standard Edition
 * Microsoft Visual FoxPro 6.0 Professional Edition
 * Microsoft Visual FoxPro 7.0 Professional Edition
 * Microsoft Visual FoxPro 8.0 Professional Edition
 * Microsoft Visual FoxPro 9.0 Professional Edition

-



This article was previously published under Q138205



SUMMARY
This article demonstrates using OLE Automation in Microsoft Visual FoxPro to send data from a table to a Microsoft Word document. The example requires that Microsoft Word 6.0 or later be installed on the same computer as Visual FoxPro. This example consists of the following two basic steps:
 * Create the mail merge document in Microsoft Word.
 * Write the program to perform the OLE automation in Visual FoxPro.



MORE INFORMATION
  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,



Sincerely,

 Create bookmarks for Contact, Company, Phone, and greeting.  Select the item in the document. For example, select Contact. Click Edit or click Insert depending on your version of Word, and then click Bookmark. Type the bookmark name, such as contact, and then 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. </li> Create the OLE Automation program in Visual FoxPro. Create a new program in Visual FoxPro with the following code:

If you are using Word 6.0 or Word 95, use the following code:

PUBLIC oWordObj, ver_info

ver_info=IIF("06."$VERSION,6,5) IF ver_info=6 OPEN DATA home(2)+"data\TestData.dbc" else OPEN DATA SYS(2004)+"\samples\data\TestData.dbc" endif USE Customer

oWordObj=CREATEOBJECT("Word.Basic")

IF ver_info=6 oWordObj.FileOpen (home(2)+"data\wordole.doc") ELSE oWordObj.FileOpen (SYS(2004)+"samples\data\wordole.doc") ENDIF

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

If you are using Word 97 or Office Word 2003, use the following code:

PUBLIC oWordObj, ver_info

ver_info=IIF("06."$VERSION,6,5) IF ver_info=6 OPEN DATA home(2)+"data\TestData.dbc" else OPEN DATA SYS(2004)+"\samples\data\TestData.dbc" endif USE Customer

oWordObj=CREATEOBJECT("Word.Basic")

IF ver_info=6 oWordObj.FileOpen (home(2)+"data\wordole.doc") ELSE oWordObj.FileOpen (SYS(2004)+"samples\data\wordole.doc") ENDIF

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

</li> Save and run the program.</li></ol>

Result When you run the program, 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.

Keywords: kbhowto kbinterop kbautomation kbcode KB138205

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.