O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required