Using the Create Object Function

To create a reference to the Automation object from a VBA procedure, use the CreateObject function with the following syntax:


The argument class is the name of the application you want to reference. This name includes the object class type as discussed earlier (see the section on early binding). The Automation object must be assigned to the object variable by using the Set keyword, as shown below:

Set variable_name = CreateObject(class)

For example, to activate Word using the Automation object, include the following declaration statements in your VBA procedure:

'early binding

Dim wordAppl As Word.Document

Set wordAppl = CreateObject("Word.Application")

'late binding

Dim wordAppl As Object

Set wordAppl = CreateObject("Word.Application")

As a rule, the CreateObject function creates a new instance of the specified Automation object. Some applications, however, register themselves as so-called "single-instance" applications. This means that you cannot run more than one instance of the application at a time. Microsoft Word and PowerPoint are such single-instance applications. Therefore, if Word or PowerPoint are already running, the CreateObject function will simply reference a running instance instead of creating a new instance.

0 0

Post a comment