Because many COM components do not yet support the As New keyword, you
will need to create and instantiate object variables for these components' classes with
standard variable declarations and the CreateObject or GetObject functions.
The CreateObject and GetObject functions return a reference to a server class
object. Use them with the Set keyword to assign their return values to a previously declared
class object variable:
Set objExcel = GetObject(,"Excel.Application")
or
Set objExcel = CreateObject("Excel.Application")
The CreateObject function takes a single required argument, which is the name
of the class you're instantiating. It always instantiates a new object in your application.
It also takes a second optional argument which is a string representing the share name of
the server where you can create a remote object.
GetObject
You can use GetObject to create an object from an already running instance
of a server.
GetObject takes two possible parameters. You must always specify at least one of the two parameters:
GetObject's first parameter is a String giving the path and filename of a data
file associated with the server application and its class.
GetObject's second parameter is the same as CreateObject's single parameter:
the name of the class you're instantiating.
There are several rules to keep in mind when using GetObject's parameters:
If you leave the first parameter completely blank (that is a single comma before the
second argument), GetObject will always reference an existing object. If there
is no existing object, a runtime error occurs.
If you specify a valid filename in the first parameter and the file is of the
type associated with the server application, you may leave the second parameter blank.
GetObject will open the file with the associated server application. GetObject
will use an existing reference to the object if it exists, or it will open a
new copy of the object if none existed before in the application.
If you specify a blank filename (" ") in GetObject's first parameter,
then you must specify the second parameter. GetObject will then always open a new
copy of the object regardless of whether one already exists in the application.
The possible configurations of these two parameters are summarized in
Table 10.1.
TABLE 10.1POSSIBLE COMBINATIONS OF FILENAME AND SERVER.CLASS ARGUMENT SETTINGS
FOR GETOBJECT FUNCTION
FileName
|
Server.Class
|
Effect
|
| Blank |
Blank |
Not a possible combination. |
| Blank |
Server.Class |
Always uses an existing instance.
Runtime error if there is no existing instance. |
| Empty String |
Server.Class |
Always opens a new instance of the server. |
| FileName | Blank |
Opens server of type associated with FileName.
Uses instance if Available otherwise opens new
instance. |
| FileName | Server.Class | file specified with a new instance of
the server. |
IMPORTANT - Limitations on Support for GetObject: The following
discussion of GetObject applies to many Microsoft products and other COM components. However,
the use of GetObject is applicationspecific. In fact, some ActiveX server applications
don't support GetObject at all. You should refer to the application's documentation to
find out whether GetObject is supported and, if so, what the proper syntax would be for
using GetObject. If GetObject isn't available, you must always use CreateObject
Comparing GetObject and CreateObject
Since several exam questions usually rely on the confusion between GetObject
and CreateObject, it is useful to emphasize the difference between these two functions:
-
CreateObject always creates a new instance of the
server object.
-
GetObject can use a running instance of the server
object but can also create a new instance depending on the syntax you use, as
detailed in the following section.