Name

CreateObject Function

Class

Microsoft.VisualBasic.Interaction

Named Arguments

No

Syntax

                  objectvariable = CreateObject(progid [, servername])
objectvariable (required; Object)

A variable to hold the reference to the instantiated object

progid (required; String)

The programmatic identifier (or ProgID) of the class of the object to create

servername (optional; String)

The name of the server on which the object resides

Return Value

A reference to a COM or ActiveX object

Description

Creates an instance of an OLE Automation (ActiveX) object.

Prior to calling the methods, functions, or properties of a COM or ActiveX object, you are required to create an instance of that object. Once an object is created, reference it in code using the object variable you defined.

Rules at a Glance

  • If your project does not include a reference to the object, you must declare the object variable type as Object; this allows the variable to reference any type of object.

  • If an instance of the ActiveX object is already running, CreateObject may start a new instance when it creates an object of the required type.

  • CreateObject can only be used to create instances of COM (or ActiveX) objects; it cannot be used to instantiate .NET components.

Example

The following routine defines a generic Object variable, as well as an Excel application object. It then uses the Timer function to compare the performance of the code fragment that uses late binding to instantiate the Excel application object with the one that uses early ...

Get VB.NET Language in a Nutshell, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.