HOWTO: Automating a Word Object Embedded in a Form

Last reviewed: February 20, 1997
Article ID: Q161197
The information in this article applies to:
  • Microsoft Visual FoxPro for Windows, version 5.0

SUMMARY

This article provides an example of how to programmatically update an OLE object embedded within a Word document. This example uses both Visual FoxPro 5.0 and Microsoft Word for Windows 95 version 7.0a.

MORE INFORMATION

The sample creates a new form and adds a Word object to that form. Next, the code activates the Word object to get a reference to its WordBasic object, deactivates it, and then automates the WordBasic object. The important part of this code is the temporary activation of Word. The object embedded in a form is called "Document." Unfortunately, Word does not support the object "Word.Document" through OLE Automation, but Word does support the object "WordBasic." The following line of code saves a reference to the object contained within the Word document:

   oWordRef = oForm.oWordDoc.Object.Application.WordBasic

This allows a programmer to update the object.

Please see the REFERENCES section of this article for additional information.

Code Example

Place the following code in a program file and run it:

   #DEFINE CRLF  CHR(13)+CHR(10)
   PUBLIC oForm
   oForm = CREATE('form')
   oForm.AddObject('oWordDoc','OleControl','WordDocument')
   oForm.oWordDoc.Height =  100
   oForm.oWordDoc.Width =  300
   oForm.oWordDoc.Visible =  .t.
   oForm.Show
   oForm.oWordDoc.DoVerb(0)
   oWordRef = oForm.oWordDoc.Object.Application.WordBasic
   MOUSE CLICK AT 0,0
   oWordRef.Insert("Hello World"+CRLF)
   oWordRef.EditSelectAll
   oWordRef.Font("Arial",18)
   oWordRef.Bold
   oWordRef.EditGoTo("\EndofDoc")
   oWordRef.WordLeft(3)
   oWordRef.SelectCurWord
   oWordRef.CharColor(2)
   RETURN

This code will replace the Visual FoxPro toolbars with the Word toolbars in order to edit the embedded Word object.

REFERENCES

For more information about OLE Automation, Visual FoxPro, and Word, please see the following articles in the Microsoft Knowledge Base:

   ARTICLE-ID: Q138205
   TITLE     : How To Use OLE Automation with Microsoft Word

   ARTICLE-ID: Q148474
   TITLE     : SAMPLE: OLE Automation to Microsoft Excel, Word, &
               PowerPoint

   ARTICLE-ID: Q105535
   TITLE     : Word for Windows Supports OLE Automation as a Server Only


KBCategory: kbprg kbole kbhowto
KBSubcategory: FxinteropOle VFoxWin
Additional reference words: 5.00


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 20, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.