Chapter 22: Integrating Access with Other Applications
799
For example, to work with an existing instance of Microsoft Word, but not a specific Word docu-
ment, you can use the following code:
Dim WordObj as Word.Application
Set WordObj = GetObject(, “Word.Application”)
To get an instance of an existing Word document called MyDoc.Docx, on your C: drive, you can
use the following code:
Dim WordObj as Word.Application
Set WordObj = GetObject(“C:\MyDoc.Docx”, “Word.Application”)
Of course, this code is placed in a function or sub that you add to a code module.
Caution
The behavior of GetObject might have unexpected side effects. If, for example, Microsoft Word is already open
on the user’s computer, GetObject returns a handle to the running instance of Word, even if the user is work-
ing with Word at the same time that the Automation code runs. Obviously, this might cause confusion to the user
as Word loads a new document and begins making changes to the document with no input from the user.
Working with Automation objects
After you have a valid instance of an Automation server, you manipulate the object as if you were
working directly with the server application in Windows.
For example, when developing directly in Word, you can use the following code to change the
directory that Word uses when opening an existing file:
Dim WordObj As Word.Application
Set WordObj = New Word.Application
WordObj.ChangeFileOpenDirectory “C:\My Documents\”
Note
Consult the development help for the Automation server (Word, Excel, and so on) for specific information on
the objects, properties, and methods available.
Tip
When using Automation, avoid setting properties or calling methods that cause the Automation server to ask for
input from the user via a dialog box. When a dialog box is displayed, the Automation code stops executing until
the dialog box is closed. If the server application is minimized or behind other windows, the user might not even
be aware that she needs to provide input and, therefore, might assume that the application is locked up.