ACC2000: How to Use Automation to Find Bookmarks in Word 2000 Documents (209966)



The information in this article applies to:

  • Microsoft Access 2000
  • Microsoft Word 2000

This article was previously published under Q209966
Moderate: Requires basic macro, coding, and interoperability skills.

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

SUMMARY

This article describes how to use Automation in Microsoft Access 2000 to open a Microsoft Word 2000 document, and how to move to a specific location in the document.

MORE INFORMATION

By using the Automation code in Microsoft Access, you can open a Microsoft Word document and move to a bookmark location in the document.

The following examples assume that you have Microsoft Word 2000 set up on your computer, that you have a document called C:\My documents\WordTest.doc, and that the document contains a pre-defined bookmark called City. Example one opens a Word 2000 document that is external to Access; example two opens a document that is embedded in an Access form.

Example 1: Finding a Bookmark in an External Microsoft Word 2000 Document

  1. Start Microsoft Access and open the sample database Northwind.mdb.
  2. Create a new module in Design view.
  3. On the Tools menu, click References.
  4. Click to select the Microsoft Word 9.0 Object Library check box in the Available References box. If that selection does not appear, click the Browse button and look for a file called Msword9.olb, which is installed in the C:\Program Files\Microsoft Office\Office folder by default.
  5. Click OK in the References dialog box.
  6. Type or paste the following procedure in the module:
    Function FindBMark()
       Dim WordObj As Word.Application
       Dim WordDoc As Word.Document
       Dim WordRange As Word.Range
       Set WordObj = CreateObject("Word.Application")
       Set WordDoc = WordObj.Documents.Open _
           ("C:\My Documents\Wordtest.doc")
       WordObj.Visible = True
    
       ' Go to the bookmark named "City."
       Set WordRange = WordDoc.Goto(What:=wdGoToBookmark, Name:="City")
       WordRange.InsertAfter "Los Angeles"
    
       ' Uncomment the next line of code to print the document.
       ' WordDoc.PrintOut Background:=False
    
       ' Uncomment the next line of code to save the modified document.
       ' WordDoc.Save
    
       ' Uncomment the line of code to quit Microsoft Word without
    
       ' saving changes to the document.
       ' WordObj.Quit SaveChanges:=wdDoNotSaveChanges
    
       Set WordObj = Nothing
    End Function
    					
  7. To test this function, type the following line in the Immediate Window (press CTRL+G), and then press ENTER:

    ? FindBMark()

    Note that Microsoft Word 2000 starts, displays the document, and then inserts the words "Los Angeles" in the document just after the bookmark called City.

Example 2: Find a Bookmark in an Embedded Microsoft Word 2000 Document

  1. Start Microsoft Access and open the sample database Northwind.mdb.
  2. Open any module in Design view.
  3. On the Tools menu, click References.
  4. Click to select the Microsoft Word 9.0 Object Library check box in the Available References box. If that selection does not appear, click the Browse button and look for a file called Msword9.olb, which is installed in the C:\Program Files\Microsoft Office\Office folder by default.
  5. Click OK in the References dialog box.
  6. Create a new form not based on any table or query in Design view.
  7. Add an unbound object frame control to the Detail section of the form.
  8. When the Insert Object dialog box appears, click Create From File, and then click the Browse button to select your C:\My Documents\WordTest.doc file.
  9. Click Open in the Browse dialog box, and then click OK in the Insert Object dialog box.
  10. Set the following properties for the unbound object frame control:

    Unbound Object Frame:
    ----------------------
    Name: UnboundObj
    Locked: No

  11. Add a command button to the form; set its Name property to EditWordDoc and set its OnClick property to the following event procedure:
    Private Sub EditWordDoc_Click()
       Dim WordObj As Word.Application
       Dim WordDoc As Word.Document
       Dim WordRange As Word.Range
    
       ' Open Microsoft Word 97 in place and activate it.
       Me![UnboundObj].Verb = -4
       Me![UnboundObj].Action = 7
    
       Set WordObj = Me![UnboundObj].Object.Application
       Set WordDoc = WordObj.ActiveDocument
       Set WordRange = WordDoc.Goto(What:=wdGoToBookmark, Name:="City")
       WordRange.InsertAfter "Los Angeles"
       Set WordObj = Nothing
    End Sub
    					
  12. Save the form as frmBookmark, and then open it in Form view.
  13. Click the command button on the form and note that the document is edited in place on the form, and that the words "Los Angeles" are inserted after the City bookmark.

REFERENCES

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

For more information about Verb and Action properties, 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 bookmarks, click Microsoft Word 2000 Help on the Help menu, type add bookmarks in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

Modification Type:MajorLast Reviewed:6/28/2004
Keywords:kbhowto kbinfo kbusage KB209966