CreateObject Function PDF
CreateObject Function PDF
Part
Description
class
Required; Variant String. The application name and class of the object to create.
servername
Optional; Variant String. The name of the network server where the object will be created. If servername is an empty string "", the local machine is used.
The class argument uses the syntax appname.objecttype and has these parts:
Part
Description
appname
Required; Variant String. The name of the application providing the object.
objecttype
Remarks
Every application that supports Automation provides at least one type of object. For example, a word processing application may provide an Application object, a Document
To create an ActiveX object, assign the object returned by CreateObject to an object variable:
VBA
'Declareanobjectvariabletoholdtheobject
'reference.DimasObjectcauseslatebinding.
DimExcelSheetAsObject
SetExcelSheet=CreateObject("Excel.Sheet")
This code starts the application creating the object, in this case, a Microsoft Excel spreadsheet. Once an object is created, you reference it in code using the object variable you define
properties and methods of the new object using the object variable, ExcelSheet, and other Microsoft Excel objects, including the Application object and the Cells collection.
VBA
'MakeExcelvisiblethroughtheApplicationobject.
ExcelSheet.Application.Visible=True
'Placesometextinthefirstcellofthesheet.
ExcelSheet.Application.Cells(1,1).Value="ThisiscolumnA,row1"
'SavethesheettoC:\test.xlsdirectory.
ExcelSheet.SaveAs"C:\TEST.XLS"
'CloseExcelwiththeQuitmethodontheApplicationobject.
ExcelSheet.Application.Quit
'Releasetheobjectvariable.
SetExcelSheet=Nothing
Declaring an object variable with the AsObject clause creates a variable that can contain a reference to any type of object. However, access to the object through that variable is lat
when your program is run. To create an object variable that results in early binding, that is, binding when the program is compiled, declare the object variable with a specific class ID.
create the following Microsoft Excel references:
VBA
DimxlAppAsExcel.Application
DimxlBookAsExcel.Workbook
DimxlSheetAsExcel.WorkSheet
SetxlApp=CreateObject("Excel.Application")
SetxlBook=xlApp.Workbooks.Add
SetxlSheet=xlBook.Worksheets(1)
The reference through an earlybound variable can give better performance, but can only contain a reference to the class specified in the declaration.
You can pass an object returned by the CreateObject function to a function expecting an object as an argument. For example, the following code creates and passes a reference to a
VBA
CallMySub(CreateObject("Excel.Application"))
You can create an object on a remote networked computer by passing the name of the computer to the servername argument of CreateObject. That name is the same as the Machin
share named "\\MyServer\Public," servername is "MyServer."
Note
Refer to COM documentation see Microsoft Developer Network for additional information on making an application visible on a remote networked computer. You may have to add
The following code returns the version number of an instance of Excel running on a remote computer named MyServer:
VBA
DimxlAppAsObject
SetxlApp=CreateObject("Excel.Application","MyServer")
Debug.PrintxlApp.Version
If the remote server doesn't exist or is unavailable, a runtime error occurs.
Note
Use CreateObject when there is no current instance of the object. If an instance of the object is already running, a new instance is started, and an object of the specified type is crea
start the application and have it load a file, use the GetObject function.
If an object has registered itself as a singleinstance object, only one instance of the object is created, no matter how many times CreateObject is executed.
Example
This example uses the CreateObject function to set a reference xlApp to Microsoft Excel. It uses the reference to access the Visible property of Microsoft Excel, and then uses the
it. Finally, the reference itself is released.
VBA
DimxlAppAsObject'Declarevariabletoholdthereference.
SetxlApp=CreateObject("excel.application")
'YoumayhavetosetVisiblepropertytoTrue
'ifyouwanttoseetheapplication.
xlApp.Visible=True
'UsexlApptoaccessMicrosoftExcel's
'otherobjects.
2016 Microsoft