The information in this article applies to:
- Microsoft Access versions 7.0, 97
- Microsoft Word 97
SUMMARY
Moderate: Requires basic macro, coding, and interoperability skills.
This article shows how to use Automation in Microsoft Access to open a
Microsoft Word 97 document and move to a specific location in the document.
For information about how to move to a specific location in a Microsoft
Word version 6.0 or 7.0 document, please see the following article in the
Microsoft Knowledge Base:
ARTICLE-ID: Q154568
TITLE : ACC: Finding a Location in a Word Document Using Automation
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 code in Microsoft Access, you can open a Microsoft Word
97 document and move to a bookmark location in the document.
The following examples use two methods, one that opens a Microsoft Word 97
document that is external to Microsoft Access, and one that opens a
document that is embedded in a Microsoft Access form.
These examples assume that you have Microsoft Word 97 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 1: Find a Bookmark in an External Microsoft Word 97 Document
- Open the sample database Northwind.mdb.
- Create a new module in Design view.
- On the Tools menu, click References.
- Click Microsoft Word 8.0 Object Library in the Available References box.
If that selection does not appear, click the Browse button and look for
a file called Msword8.olb, which is installed in the C:\Program
Files\Microsoft Office\Office folder by default.
- Click OK in the References dialog box.
- Type 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
- To test this function, type the following line in the Debug window,
and then press ENTER.
?FindBMark()
Note that Microsoft Word 97 opens, 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 97 Document
- Open the sample database Northwind.mdb.
- Open any module in Design view.
- On the Tools menu, click References.
- Click Microsoft Word 8.0 Object Library in the Available References
box. If that selection does not appear, click the Browse button and
look for a file called Msword8.olb, which is installed in the
C:\Program Files\Microsoft Office\Office folder by default.
- Click OK in the References dialog box.
- Create a new form not based on any table or query in Design view.
- Add an unbound object frame control to the detail section of the form.
- 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.
- Click Open in the Browse dialog box, and then click OK in the Insert
Object dialog box.
- Set the following properties for the unbound object frame control:
Unbound Object Frame:
Name: UnboundObj
Locked: No
- 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
- Save the form as frmBookmark, and then open it in Form view.
- 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, search the Help Index for
"Automation," or ask the Microsoft Access 97 Office Assistant.
For more information about Verb and Action properties, search the Help
Index for "Verb property" or "Action property," or ask the Microsoft Access
97 Office Assistant.
For more information about in-place activation, search the Help Index for
"in-place activation," or ask the Microsoft Access 97 Office Assistant.
For more information about bookmarks, search the Microsoft Word 97 Help
Index for "bookmarks," or ask the Microsoft Word 97 Office Assistant.