Microsoft KB Archive/210032

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

Article ID: 210032

Article Last Modified on 6/23/2005

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition
 * Microsoft Word 2000 Standard Edition

-



This article was previously published under Q210032



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
 * Example: Go to a Bookmark in a Microsoft Word Document
 * Example: Go to a Bookmark in a Word Document in a Form's Object Frame

REFERENCES



SUMMARY
This article shows how to use Automation to move to a specific location 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 a Microsoft 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.  Start Microsoft Access and open any database. Create a new module, and set a reference to the Microsoft Word 9.0 Object Library. To do so, follow these steps:  On the Tools menu, click References. 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. Click OK.   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 = Nothing End Function </li>  Press CTRL+G to open the Immediate window, type the following line, and then press ENTER: ? FindBMark </li> Start Microsoft Word and open the document C:\Wordtest.doc. Note the text "Los Angeles" inserted at the bookmark.</li></ol>

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:  Start Microsoft Access and open any database.</li> Create 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 C:\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, and set a reference to the Microsoft Word 9.0 Object Library.</li>  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 = Nothing End Function </li> 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.</li></ol>

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

<div class="references_section">