At its simplest,
CreateObject creates an instance of an object whereas
GetObject gets an existing instance of an object. Determining whether an object can be created or gotten will depend on it's Instancing property. Some objects are SingleUse (eg, WMI) and cannot be created if they already exist. Other objects (eg, Excel) are MultiUse and allow multiple instances to run at once. If an instance of an object does not already exist and you attempt
GetObject, you will receive the following trappable message:
Run-time error '429': ActiveX component can't create object.
GetObject requires at least one of these two optional parameters to be present:
- Pathname - Variant (String): The full path, including filename, of the file containing the object. This parameter is optional, but Class is required if Pathname is omitted.
- Class - Variant (String): A string representing the formal definition (Application and ObjectType) of the object. Class is required if Pathname is omitted.
CreateObject has one required parameter and one optional parameter:
- Class - Variant (String): A string representing the formal definition (Application and ObjectType) of the object. Class is a required parameter.
- Servername - Variant (String): The name of the remote computer on which the object will be created. If omitted, the object will be created on the local machine.
Class is always comprised of two parts in the form of
- Application - The name of the application which the object is part of. |
- Object Type - The type of object being created. |
Some example classes are:
Copying, returning and passing arrays
Demonstrating GetObject and CreateObject
This modified text is an extract of the original Stack Overflow Documentation created by following contributors
and released under CC BY-SA 3.0