CreateObject (function)

Syntax

CreateObject(class$)

Description

Creates an OLE automation object and returns a reference to that object.

Comments

The class$ parameter specifies the application used to create the object and the type of object being created. It uses the following syntax:

 "application.class",

where application is the application used to create the object and class is the type of the object to create.

At runtime, CreateObject looks for the given application and runs that application if found. Once the object is created, its properties and methods can be accessed using the dot syntax (e.g., object.property = value).

There may be a slight delay when an automation server is loaded (this depends on the speed with which a server can be loaded from disk). This delay is reduced if an instance of the automation server is already loaded.

Examples

This first example instantiates Microsoft Excel. It then uses the resulting object to make Excel visible and then close Excel.

Sub Main()

 Dim Excel As Object

 On Error GoTo Trap1                    'Set error trap.

 Set Excel = CreateObject("excel.application"'Instantiate object.
 Excel.Visible = True                   'Make Excel visible.
 Sleep 5000                             'Wait 5 seconds.
 Excel.Quit                             'Close Excel.

 Exit Sub                               'Exit before error trap.

Trap1:

MsgBox "Can't create Excel object."     'Display error message.
Exit Sub                                'Reset error handler.
End Sub 

 

This second example uses CreateObject to instantiate a Visio object. It then uses the resulting object to create a new document.

Sub Main()

 Dim Visio As Object
 Dim doc As Object
 Dim page As Object
 Dim shape As Object

 On Error Goto NO_VISIO

 Set Visio = CreateObject("visio.application")'Create Visio object.
 On Error Goto 0

 Set doc = Visio.Documents.Add("")       'Create a new document.

 Set page = doc.Pages(1)                 'Get first page.
 Set shape = page.DrawRectangle(1,1,4,4) 'Create a new shape.
 shape.text = "Hello, world."            'Set text within shape.
 End
NO_VISIO:
 MsgBox "'Visio' cannot be found!",ebExclamation
End Sub

See Also

GetObject (function); Object (data type).

More information

C