Microsoft KB Archive/154568

= ACC: How to Use Automation to Find a Location in a Word Document =

Article ID: 154568

Article Last Modified on 1/19/2007

-

APPLIES TO


 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition
 * Microsoft Word 95 Standard Edition
 * Microsoft Word 6.0 Standard Edition

-



This article was previously published under Q154568



Moderate: Requires basic macro, coding, and interoperability skills.



SUMMARY
This article shows you how to use Automation to move to a specific location in a Microsoft Word 6.0 or 7.0 document.

For more information on how to use Automation to move to a specific location in a Microsoft Word 97 document, please see the following article here in the Microsoft Knowledge Base:

160295 ACC: How to Use Automation to Find Bookmark in Word 97 Document

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.



MORE INFORMATION
By using Automation, it is possible to go to a specific location in a Microsoft Word 6.0 or 7.0 document. This example uses the WordBasic EditGoto statement in Microsoft Word for Windows.

Example: Go to a Bookmark in a Microsoft Word Document
The following example opens a Microsoft Word document called Wordtest.doc, moves to a pre-defined bookmark, and then inserts some text. The following code samples assume that a document, C:\My Documents\Wordtest.doc, exists and that it contains a pre-defined bookmark, "city" (without the quotation marks).

To run this code, follow these steps:

 Open Word. When the code runs from Microsoft Access, Word remains open. Open a Microsoft Access database and create a new module.  Type the following procedure: Function FindBMark Dim WordObj as Object Set WordObj = CreateObject("Word.Basic") WordObj.FileOpen "C:\My Documents\Wordtest.doc" ' Go to the bookmark named "City." WordObj.EditBookmark "City", 0, 0, 0, 1 WordObj.Insert "Los Angeles" End Function  To test this function, type the following line in the Debug window, and then press ENTER:

? FindBMark



Example: Go to a Bookmark in a Word Document in a Form's Object Frame
This procedure works with an embedded Microsoft Word object on a form. The OLE control on the form is called OLEObj.

To run this code:

 Open a new, blank form in Design view.</li> Create an unbound object frame on the form. When prompted with the Insert Object dialog box, click the Create From File button and choose the Microsoft Word document that contains your bookmarks ("C:\My Documents\Wordtest.doc"). Click OK.</li> Display the property sheet for the object frame control. Set the Name property to OLEObj.</li> Add a command button to the form with the following properties:

Command button:

Caption: Find Bookmark

OnClick: =FindBookMark

</li> On the View menu, click Code to open the form's module.</li>  Type the following procedure: Function FindBookMark Dim WordObj as Object Me![OLEObj].Verb = -2  ' Tells MS Access to open the application. Me![OLEObj].Action = 7 ' Activates the application. Set WordObj = Me![OLEObj].Object.Application.WordBasic ' Goes to the pre-defined bookmark named "City" and inserts "Los        ' Angeles." WordObj.EditGoto "city" WordObj.Insert "Los Angeles" WordObj.FileClose End Function </li> To run the code, open the form in Form view and click the command button. Microsoft Word becomes the active application, moves the insertion point to the bookmark, and inserts the text. To update the object frame's contents, on the File menu, click "Close and Return to Formx: Form."</li></ol>

NOTE: In both examples, when the Automation object variable loses scope, the instance of Microsoft Word is unloaded, unless the object was created from a previous instance (already opened).

Instead of using the EditGoto statement to go to a bookmark, you can also use the EditBookmark statement. For more information about the WordBasic EditBookmark command, search for "EditBookmark," using the Microsoft Word 7.0 Help Index. You must have the WordBasic Help file, Wrdbasic.hlp, installed.

<div class="references_section">