Creates and returns a reference to a COM object. CreateObject cannot be used to create instances of classes in Visual Basic unless those classes are explicitly exposed as COM components.
Public Shared Function CreateObject( _ ByVal ProgId As String, _ Optional ByVal ServerName As String = "" _ ) As Object
| Exception type | Error number | Condition |
|---|---|---|
| 429 | ProgId not found or not supplied. | |
| 462 | Server is unavailable | |
| 53 | No object of the specified type exists. |
To create an instance of a COM component, assign the object returned by CreateObject to an object variable:
Sub CreateADODB()
Dim adoApp As Object
adoApp = CreateObject("ADODB.Connection")
End Sub
The type of object variable you use to store the returned object can affect your application's performance. Declaring an object variable with the As Object clause creates a variable that can contain a reference to any type of object. However, access to the object through that variable is late bound; that is, the binding occurs when your program is run. There are many reasons you should avoid late binding, including slower application performance. To create an object variable that results in early binding, that is, binding when the program is compiled, add a reference to the type library for your object from the COM tab of the Add Reference dialog box on the Project menu and declare the object variable of the specific type of your object. In most cases, it is more efficient to use the Dim statement and a primary interop assembly to create objects than it is to use the CreateObject function.
Another issue is that COM objects use unmanaged code code without the benefit of the common language runtime. There is a fair degree of complexity involved in mixing the managed code of Visual Basic .NET with unmanaged code from COM. When you add a reference to a COM object, a search is made for a Primary Interop Assembly for that library; if one is found then it is used. If none is found Visual Basic .NET creates an interoperability assembly that contains local interoperability classes for each class in the COM library. For more information, see
You can create an object on a remote networked computer by passing the name of the computer to the ServerName argument of the CreateObject function. That name is the same as the Machine Name portion of a share name: for a share named "\\MyServer\Public," ServerName is "MyServer."
Note Refer to COM documentation (see Microsoft Developer Network) for additional information on making an application accessible on a remote networked computer. You may need to add a registry key for your application.
The following code returns the version number of an instance of Excel running on a remote computer named MyServer:
Sub CreateRemoteExcelObj()
Dim xlApp As Object
' Replace string "\\MyServer" with name of the remote computer.
xlApp = CreateObject("Excel.Application", "\\MyServer")
MsgBox(xlApp.Version)
End Sub
If the remote server name is incorrect, or if it is unavailable, a run-time 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 created. To use the current instance, or to start the application and have it load a file, use the GetObject function. If an object has registered itself as a single-instance object, only one instance of the object is created, no matter how many times CreateObject is executed.
This example uses the CreateObject function to create a Microsoft Excel worksheet and saves the worksheet to a file. To use this example, Excel must be installed on the computer where this program will run, and you must add a reference to the type library from the COM tab of the Add Reference dialog box on the Project menu. The name of the type library will vary depending on the version of Excel installed on your computer. For example, the type library for Microsoft Excel 2002 is named Microsoft Excel 10.0 Object Library.
Sub TestExcel()
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
xlApp = CType(CreateObject("Excel.Application"), Excel.Application)
xlBook = CType(xlApp.Workbooks.Add, Excel.Workbook)
xlSheet = CType(xlBook.Worksheets(1), Excel.Worksheet)
' Place some text in the second row of the sheet.
xlSheet.Cells(2, 2) = "This is column B row 2"
' Show the sheet.
xlSheet.Application.Visible = True
' Save the sheet to C:\Test.xls directory.
xlSheet.SaveAs("C:\Test.xls")
' Optionally, you can call xlApp.Quit to close the work sheet.
End Sub
Namespace: Microsoft.VisualBasic
Module: Interaction
Assembly: Microsoft Visual Basic .NET Runtime (in Microsoft.VisualBasic.dll)
GetObject Function | Dim Statement | Declare Statement |