VBA CreateObject vs. GetObject

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

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


Got any VBA Question?