Create COM Object Method
The Create COM Object method creates a new COM object. It does not return a value.
Format
CreateObject(application.objectname)
The following table describes the arguments that you can use with this method.
Argument | Description |
---|---|
application |
The name of the application. |
objectname |
The name of the object that this method uses. |
Usage
To create an object, you use the Dim statement to declare an object variable, and then set the variable equal to the new object. For example:
Dim excelObj As Object
Set excelObj = CreateObject("Excel.Application")
You can use one of the following formats to reference a method or property of the object:
objectvar.property
objectvar.method
For example:
Dim cellVal as String
cellVal = excelObj.ActiveSheet.Cells(1,1).Value
You cannot display a modal or nonmodal form from a server application. A DLL that this method instantiates must be thread-safe.
To identify correct application and object names, see the documentation for your Web Client Automation Server application.
If a method passes the wrong number, order, or type of arguments to a COM object when it calls a COM object, then a 440 error message might occur.
Example 1
The following example uses the Create COM Object method to create a Microsoft Excel worksheet. It then edits and saves this worksheet:
Sub BtnExcel_Click
Dim oWorkSheet As Object
Dim sfileName As String
Set oWorkSheet = CreateObject("Excel.Sheet")
If oWorkSheet Is Nothing then
Exit Sub
End If
' Make Excel visible through the Application object.
oWorkSheet.Application.Visible = 1
' Place some text in the first cell of the sheet
oWorkSheet.ActiveSheet.Cells(1,1).Value = "Column A, Row 1"
' Save the sheet
sfileName = "C:\demo.xls"
oWorkSheet.SaveAs (fileName)
' Close Excel with the Quit method on the Application object
oWorkSheet.Application.Quit
' Clear the object from memory
Set oWorkSheet = Nothing
End Sub
The following example uses the Create COM Object method to create a Microsoft Word document. It then edits and saves this document:
Sub BtnWrd_Click
Dim oWord As Object
Dim fileName As String
fileName = "C:\demo.doc"
Set oWord = CreateObject("Word.Application")
' Create a new document
oWord.Documents.Add
If oWord Is Nothing then
Exit Sub
End If
' Make Word visible through the Application object
oWord.Application.Visible = 1
' Add some text
oWord.Selection.TypeText "This is a demo."
' Save the document
oWord.ActiveDocument.SaveAs (fileName)
' Close Word with the Quit method on the Application object
oWord.Quit
' Clear the object from memory
Set oWord = Nothing
End Sub