Stats

479 Contributors: 3 Friday, February 3, 2017
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial: riptutorial@gmail.com
Roadmap: roadmap

CreateObject vs. GetObject

Remarks

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:

  1. 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.
  2. 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:

  1. Class - Variant (String): A string representing the formal definition (Application and ObjectType) of the object. Class is a required parameter.
  2. 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.ObjectType:

  1. Application - The name of the application which the object is part of. |
  2. Object Type - The type of object being created. |

Some example classes are:

  1. Word.Application
  2. Excel.Sheet
  3. Scripting.FileSystemObject

Related Examples