Visual Basic Concepts

Assigning an Object Reference to a Variable

See Also

After you declare an object variable, you must assign an object reference to the variable before you can use the object's properties, methods, and events. You can assign a new object reference in several ways:

Assigning an Object Reference Using the New Keyword

If the ActiveX component supplies a type library, you can use the New keyword in a variable declaration or Set statement to create a new object and assign an object reference to an object variable.

If you declare an object variable with the New keyword, Visual Basic will automatically create a new object the first time you use the variable. For more information, see "Declaring an Object Variable."

You can also use the New keyword in a Set statement to assign a reference to a new object of the specified class. For example, the following statements assign a reference to a new DAO table object to the variable tdfOrders, setting the table's Name property to "Orders":

Dim tdfOrders As DAO.TableDef
Set tdfOrders = New DAO.TableDef
tdfOrders.Name = "Orders"

For More Information   See "Dim Statement" or "Set Statement."

Assigning an Object Reference Using CreateObject

Regardless of whether or not an ActiveX component supplies a type library, you can use the CreateObject function in a Set statement to create a new object and assign an object reference to an object variable. You must specify the object's programmatic identifier as an argument to the function, and the object you want to access must be externally creatable.

To assign an object reference using CreateObject

The progID argument is usually the fully qualified class name of the object being created; for example, Word.Document. However, progID can be different from the class name. For example, the progID for a Microsoft Excel object is "Sheet" rather than "Worksheet." The optional servername argument can be specified to create an object on a remote machine across a network. It takes the Machine Name portion of a share name. For example, with a network share named \\MyServer\Public, the servername argument would be "MyServer."

The following code example starts Microsoft Excel (if Microsoft Excel is not already running) and establishes the variable xlApp to refer to an object of the Application class. The argument "Excel.Application" fully qualifies Application as a class defined by Microsoft Excel:

Dim xlApp As Excel.Application
Set xlApp = CreateObject("Excel.Application")

For More Information   See "CreateObject Function."

Assigning an Object Reference Using GetObject

The GetObject function is most often used to assign a reference to an existing object, although you can also use it to assign a reference to a new object.

To assign a reference to an existing object, use the following syntax.

Set objectvariable = GetObject([pathname] [, progID])

The pathname argument can be the path to an existing file, an empty string, or omitted entirely. If it is omitted, then progID is required. Specifying the path to an existing file causes GetObject to create an object using the information stored in the file. Using an empty string for the first argument causes GetObject to act like CreateObject — it will create a new object of the class whose programmatic identifier is progID. The following table describes the results of using GetObject.

If the ActiveX component is running Result
Set X = GetObject(, "MySrvr.Application")
X references an existing Application object.
Set X = GetObject("", "MySrvr.Object") X references a new, externally creatable object.

If the ActiveX component is not running Result
Set X = GetObject(, "MySrvr.Object") An error is returned.
Set X = GetObject("", "MySrvr.Object") The ActiveX component (MySrvr) is started, and X references a new object.

For example, the variable wrdApp refers to a running Microsoft Word Application:

Dim wdApp As Word.Application
Set wdApp = GetObject("", "Word.Application")

Just as with CreateObject, the argument "Word.Application" is the programmatic identifier for the Application class defined by Microsoft Word. If multiple instances of Microsoft Word are running, you cannot predict to which instance wdApp will refer.

Important   You can also use GetObject to assign a reference to an object in a compound document file. A compound document file contains references to multiple types of objects. For example, a compound document file could contain a spreadsheet, text, and bitmaps.

The following example starts the spreadsheet application, if it is not already running, and opens the file Revenue.xls:

Dim xlBook As Excel.Workbook
Set xlBook = GetObject("C:\Accounts\Revenue.xls")

For More Information   See "GetObject Function."