Caution

If your code to open Word runs smoothly but you don't see an instance of Word (and should),open your Task Manager and look for the process winword.exe. If it exists,from the Immediate Window in Excel's VBE,type the following (early binding): Word.Application.Visible = True

If multiple instances of winword.exe are found,you must make each visible and close the extra instance(s) of WinWord.exe.

CreateObject Function

The CreateObject function was used in the late binding example, but can also be used in early binding. CreateObject has a class parameter consisting of the name and type of the object to be created (Name.Type).

It creates a new instance of the object; in this case, the Word application is created.

GetObject Function

The GetObject function can be used to reference an instance of Word that is already running. It creates an error if no instance can be found.

GetObject's two parameters are optional. The first parameter specifies the full path and filename to open and the second the application program:

Sub UseGetObject() Dim wdDoc As Object

Set wdDoc = GetObject(ThisWorkbook.Path & "\Chapter 16 - Automating Word.doc") wdDoc.Application.Visible = True Set wdDoc = Nothing End Sub

This example opens a document in an existing instance of Word and ensures the Word application's Visible property is true. Note that to make the document visible, you have to refer to the application object (wdDoc.Application.Visible) because wdDoc is referencing a document rather than the application.

0 0

Post a comment