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

ID: Q154568


The information in this article applies to:
  • Microsoft Access versions 7.0, 97
  • Microsoft Word for Windows 95, version 7.0
  • Microsoft Word for Windows, version 6.0


SUMMARY

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

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:

Q160295 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:

  1. Open Word. When the code runs from Microsoft Access, Word remains open.


  2. Open a Microsoft Access database and create a new module.


  3. 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 


  4. 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:

  1. Open a new, blank form in Design view.


  2. 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.


  3. Display the property sheet for the object frame control. Set the Name property to OLEObj.


  4. Add a command button to the form with the following properties:
    Command button:
    Caption: Find Bookmark
    OnClick: =FindBookMark()


  5. On the View menu, click Code to open the form's module.


  6. 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 


  7. 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."


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.


REFERENCES

For more information about using bookmarks in Microsoft Word, search on bookmarks, and then Add a bookmark using the Microsoft Word 7.0 Help Index.

For more information about Automation, search the Help Index for Automation, or working across applications, or ask the Microsoft Access 97 Office Assistant.

For more information about the WordBasic EditFind statement used in this article, search on EditFind statement using the Microsoft Word 7.0 Help Index. You must have the WordBasic help file, Wrdbasic.hlp, installed.

For more information about the WordBasic EditGoto statement used in this article, search on EditGoto statement in the Microsoft Word 7.0 Help Index. You must have the WordBasic help file Wrdbasic.hlp installed.

For more information about the Microsoft Access Verb and Action properties, search the Help Index for Verb property or Action property.

Additional query words:

Keywords : kbinterop IntpOlea
Version : WINDOWS:6.0,7.0,97
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: October 13, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.