Chapter 22: Integrating Access with Other Applications
807
Tip
Depending on the processing that’s occurring at the time, Access may take the focus back from Word. You can
help to eliminate this annoyance by preceding the Activate method with a DoEvents statement. Note,
however, that this strategy doesn’t always work.
Moving the cursor in Word
You can move the cursor in Word by using the MoveUp method of the Selection object. For
example, the following statement moves the cursor up six lines in the document:
WordObj.Selection.MoveUp wdLine, 6
Discarding the Word object instance
To release resources that are taken by an instance of an Automation object, you should always dis-
card the instance. In this example, the following code is used to discard the object instance and
remove it from memory:
Set WordObj = Nothing
This code removes the object instance but not the instance of Word as a running application. In
this example, the user needs access to the new document, so closing Word would defeat the pur-
pose of this function. You can, however, automatically print the document and then close Word. If
you do this, you may even choose not to make Word visible during this process. To close Word
without saving the changes, use the Quit method of the Application object, as follows:
WordObj.Quit SaveChanges:= wdDoNotSaveChanges
Inserting pictures by using bookmarks
You can perform other operations with bookmarks. Basically, anything that you can do within
Word you can do by using Automation. The following code locates a bookmark that marks where
a picture is to be placed and then inserts a .bmp file from disk. You can use the following code to
insert scanned signatures into letters:
WordObj.Selection.Goto What:=wdGoToBookmark, Name:=“Picture”
WordObj.ChangeFileOpenDirectory “D:\GRAPHICS\”
WordObj.ActiveDocument.Shapes.AddPicture _
Anchor:=Selection.Range, _
FileName:=“D:\GRAPHICS\PICTURE.BMP”, LinkToFile:=False,
SaveWithDocument:=True