Microsoft Office Tutorials and References
In Depth Information
Accessing an Active Word Document
Now that you can open an Office application, what if you simply need to access a program
that is already open? If you have a current instance of an application running, you don’t need
to create a new instance and use additional resources. You can activate the running applica
tion by using the GetObject function.
The following example uses early binding and the GetObject function to copy a table to an
open Word document:
Note This function requires Microsoft Word to be open for the information to be pasted
into the active document.
Dim wdApp As Word.Application
Set wdApp = GetObject(, "Word.Application”)
Set wdApp = Nothing
The GetObject function has two input parameters, both of which are optional. The first
parameter specifies a file to be opened. The second parameter specifies the application used
to open the file. If you don’t specify the first parameter, the GetObject function assumes you
want to access a currently open instance of Word. If you specify a zero-length string as the
first parameter, GetObject assumes you want to open a new instance of Word.
You can use the GetObject function, to access a current instance of Word that’s in memory.
But if there is no current instance of Word running, the GetObject function with no first
parameter causes a run-time error.
Note When any program is opened, it creates an instance of the application in the com
puter’s memory. If the same application is opened multiple times, you will see multiple
entries for the application in the Task Manager. For the best performance, it’s preferable to
use an open instance of an application, rather than creating a new instance.
The following example accomplishes the same task. However, the Word window is visible,
and the text is inserted at the insertion point:
Dim appWord As Word.Application
Dim doc As Word.Document