CreateObject Function

Named Arguments

No

Syntax

Set objectvariable = CreateObject("library.object"[, servername])

objectvariable

Use: Required

Data Type: Object

A variable to hold the reference to the instantiated object.

library

Use: Required

Data Type: String

The name of the application or library containing the object.

object

Use: Required

Data Type: String

The type or class of object to create.

servername

Use: Optional (Available in VB6 only)

Data Type: String

The name of the server on which the object resides.

Return Value

A reference to an ActiveX object.

Description

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

Rules at a Glance

  • If your project doesn't 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.

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 binding. (For a discussion of late ...

Get VB & VBA in a Nutshell: The Language 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.