GetObject Function |
Microsoft. VisualBasic.Interaction
GetObject([ pathname ] [, class ])
The full path and name of the file containing the COM (or ActiveX) object.
The class of the object. The class argument has these parts :
The name of the application.
The class of object to create, delimited from Appname by using a point ( . ). For example, Appname.Objecttype .
Returns a reference to an ActiveX object
Accesses an ActiveX server held within a specified file
Although both pathname and class are optional, at least one parameter must be supplied.
In situations where you cannot 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 CreateObject function.
If you specify pathname as a zero-length string, GetObject will return a new instance of the object unless the object is registered as single instance, in which case the current instance will be returned.
If you omit the pathname, the current instance of the object will be returned.
An error is generated if pathname is not specified and no current instance of the object can be found.
The object variable you will 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:
Dim myObject As Object 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, and you'll need to read the documentation available for the server to determine what you need to do to reference a particular part of the object. There are basically three ways in which you can access an ActiveX object:
The overall object library. This is the highest level and will give you access to all public sections of the library and all its public classes:
GetObject("C:\OtherApp\Library.lib")
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:
GetObject("C:\OtherApp\Library.lib!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")
Pay special attention to objects registered as single instance. As their type suggests, there can only ever be one instance of the object created at any one time. Calling CreateObject against a single-instance object more than once has no effect; you will still be returning 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 will be obtaining 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 will be 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 3. You can use GetObject in early binding with COM objects, as in:
Dim objExcel As Excel.Application objExcel = GetObject(, "Excel.Application")
The following table shows when to use GetObject and when to use CreateObject :
Task | Use |
---|---|
Create a new instance of an OLE server | CreateObject |
Create a subsequent instance of an already instantiated server (if the server is not registered as single instance) | CreateObject |
Obtain another reference to an already instantiated server without launching a subsequent instance | GetObject |
Launch an OLE server application and load an instance of a subobject | GetObject |
Instantiate a class created with VB | CreateObject |
Instantiate a class registered on a remote machine | CreateObject |
CreateObject Function