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."
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.
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
Press CTRL+G to open the Immediate window, type the following line, and then press ENTER:
Start Microsoft Word and open the document C:\Wordtest.doc. Note the text "Los Angeles" inserted at the bookmark.
On the View menu, click Code to open the form's module, and set a reference to the Microsoft Word 9.0 Object Library.
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
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).
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