HOW TO: Use Automation to Find a Location in a Word Document in Access 2000

This article was previously published under Q210032
This article has been archived. It is offered "as is" and will no longer be updated.
Moderate: Requires basic macro, coding, and interoperability skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).

IN THIS TASK

SUMMARY
This article shows how to use Automation to move to a specificlocation in a Microsoft Word 2000 document.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
By using Automation, it is possible to go to a specific location in aMicrosoft Word 2000 document.

NOTE: The following examples assume that a Microsoft Word 2000 document, C:\Wordtest.doc, exists, and contains a bookmark named "City."

back to the top

Example: Go to a Bookmark in a Microsoft Word Document

In the following example, you open a Microsoft Word document, C:\Wordtest.doc, move to a pre-defined bookmark, and then insert some text.
  1. Start Microsoft Access and open any database.
  2. Create a new module, and set a reference to the Microsoft Word 9.0 Object Library. To do so, follow these steps:
    1. On the Tools menu, click References.
    2. Click to select the Microsoft Word 9.0 Object Library check box in the Available References box. If that selection does not appear, browse for Msword9.olb, which is installed by default in the C:\Program Files\Microsoft Office\Office folder.
    3. Click OK.
  3. Type the following procedure:
    Function FindBMark()   Dim WordObj As Word.Application   ' Start Microsoft Word and open the document.   Set WordObj = CreateObject("Word.Application")   WordObj.Documents.Open "C:\Wordtest.doc"   ' Find the bookmark and insert the text.   WordObj.ActiveDocument.Bookmarks("City").Select   WordObj.Selection.Text = ("Los Angeles")   ' Close and save the document.   WordObj.ActiveDocument.Close SaveChanges:=wdSaveChanges   ' Quit Microsoft Word and release the object variable.   WordObj.Quit   Set WordObj = NothingEnd Function					
  4. Press CTRL+G to open the Immediate window, type the following line, and then press ENTER:
    ? FindBMark()					
  5. Start Microsoft Word and open the document C:\Wordtest.doc. Note the text "Los Angeles" inserted at the bookmark.
back to the top

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, follow these steps:
  1. Start Microsoft Access and open any database.
  2. Create a new, blank form in Design view.
  3. 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 C:\Wordtest.doc. Click OK.
  4. Display the property sheet for the object frame control. Set the Name property to OLEObj.
  5. Add a command button to the form with the following properties:
    Command button:
    ----------------------
    Caption: Find Bookmark
    OnClick: =FindBookMark()
  6. On the View menu, click Code to open the form's module, and set a reference to the Microsoft Word 9.0 Object Library.
  7. Type the following procedure:
    Function FindBookMark()      Dim wordApp As Word.Application   Dim wordDoc As Word.Document   Dim wordRange As Word.Range   Me![OLEObj].Verb = acOLEVerbInPlaceUIActivate   Me![OLEObj].Action = acOLEActivate   Set wordApp = Me![OLEObj].Object.Application   Set wordDoc = wordApp.ActiveDocument   Set wordRange = wordDoc.Goto(What:=wdGoToBookmark, Name:="City")   wordRange.InsertAfter "Los Angeles"   Set wordApp = NothingEnd Function					
  8. To run the code, open the form in Form view and click the Command button. Notice that the words "Los Angeles" are inserted after the bookmark.
NOTE: In the second example the instance of Microsoft Word is unloaded when the Automation object variable loses scope, unless the object was created from a previous instance (already opened).

back to the top


REFERENCES
For more information about using bookmarks, click Microsoft Word Help on the Help menu, type bookmarks in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

For more information about the Verb Property, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type Verb property in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

For more information about the Action property, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type Action Property in the Office Assistant or the Answer Wizard, and then click Search to view the topics returned.
For additional information about how to use Automation to move to a specific location in a Microsoft Word 2000 document, click the article number below to view the article in the Microsoft Knowledge Base:
209966 ACC2000: How to Use Automation to Find Bookmarks in Word 2000 Documents


back to the top






Properties

Article ID: 210032 - Last Review: 02/28/2014 04:17:15 - Revision: 3.0

Microsoft Access 2000 Standard Edition, Microsoft Word 2000 Standard Edition

  • kbnosurvey kbarchive kbhowto kbhowtomaster kbinterop KB210032
Feedback