Get Object Function

Named Arguments


GetObject([pathname] [, class]) pathname

Use: Optional

Data Type: Variant (String)

The full path and name of the file containing the ActiveX object.


Use: Optional

Data Type: Variant (String)

The class of the object (see next list).

The class argument has these parts:

358 Chapter 7- The Language Reference


Use: Required

Data Type: Variant (String)

The name of the application.


Use: Required

Data Type: Variant (String)

The class of object to create, delimited from Appname by using a point (.). For example, Appname.Objecttype.

Return Value

Returns a reference to an ActiveX object. Description

Accesses an ActiveX server held within a specified file. Rules at a Glance

• Although both pathname and class are optional, at least one parameter must be supplied.

• In situations in which you can't create a project-level reference to an ActiveX object, you can use the GetObject function to assign an object reference from an external ActiveX object to an object variable.

• GetObject is used when there is a current instance of the ActiveX object; to create the instance, use the CreateObjectfunction.

• If you specify pathname as a zero-length string, GetObject returns a new instance of the object—unless the object is registered as single instance, in which case the current instance is returned.

• If you omit the pathname, the current instance of the object is returned.

• An error is generated if pathname isn't specified, and no current instance of the object can be found.

• The object variable you use within your program to hold a reference to the ActiveX object is dimensioned as type Object. This causes the object to be late bound; that is, your program knows nothing of the type of object nor its interface until the object has been instantiated within your program. To assign the reference returned by GetObject to your object variable, you must use the Set statement:

Dim myObject As Object

Set myObject = GetObject("C:\OtherApp\Library.lib")

• The details of how you create different objects and classes are determined by how the server has been written; you need to read the documentation for the server to determine what you need to do to reference a particular part of the object. There are three ways you can access an ActiveX object:

GetObject Function 359

- The overall object library. This is the highest level, and it gives you access to all public sections of the library and all its public classes:


- A section of the object library. To access a particular section of the library, use an exclamation mark (!) after the filename, followed by the name of the section:


- A class within the object library. To access a class within the library, use the optional Class parameter:

GetObject("C:\OtherApp\Library.lib", "App.Class") Programming Tips & Gotchas

• Pay special attention to objects registered as single instance. As their type suggests, there can be only one instance of the object created at any one time. Calling CreateObject against a single-instance object more than once has no effect; you still return a reference to the same object. The same is true of using GetObject with a pathname of ""; rather than returning a reference to a new instance, you obtain a reference to the original instance of the object. In addition, you must use a pathname argument with single-instance objects (even if this is ""); otherwise an error is generated.

• You can't use GetObject to obtain a reference to a class created with Visual Basic.

• When possible, you should use early binding in your code. For more details on early and late binding, see Chapter 4. You can use GetObject in early binding, as in:

Dim objExcel As Excel.Application

Set objExcel = GetObject(, "Excel.Application")

The following table shows when to use GetObject and CreateObject:



Create a new instance of an OLE server


Create a subsequent instance of an already instantiated server (if the server isn't registered as single instance)


Obtain a further reference to an already instantiated server without launching a subsequent instance


Launch an OLE server application and load an instance of a subobject


Instantiate a class created with VB


Instantiate a class registered on a remote machine

CreateObject Function, Set Statement

360 Chapter 7- The Language Reference

+1 -1


    How to get the class name in createobject in vba?
    3 months ago

Post a comment