Visual Basic Language Reference  

CreateObject Function

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

Parameters

ProgId
Required. String. The program ID of the object to create.
ServerName
Optional. String. The name of the network server where the object will be created. If ServerName is an empty string (""), the local machine is used.

Exceptions/Errors

Exception type Error number Condition
Exception 429 ProgId not found or not supplied.
Exception 462 Server is unavailable
FileNotFoundException 53 No object of the specified type exists.

Remarks

To create an instance of a COM component, assign the object returned by CreateObject to an object variable:

xlApp = CreateObject("Excel.Application")

The type of object variable you use to store the returned object can affect your applications 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 a lot of reasons why 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. For example, you can declare and create the following Microsoft Excel references:

Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.WorkSheet
xlApp = CreateObject("Excel.Application")

The reference through an early-bound variable can give better performance, but can only contain a reference to the class specified in the declaration.

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 predefined interoperability assembly for that library; if one is found then it is used. If none is found you have the option of creating a local interoperability assembly that contains local interoperability classes for each class in the COM library.

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 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 visible on a remote networked computer. You may have 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:

Dim xlApp As Object
' Replace string "\\MyServer" with name of the remote computer.
   xlApp = CreateObject("Excel.Application", "\\MyServer")
   MsgBox(xlApp.Version)

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.

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 Microsoft Excel Quit method to close it. Finally, the reference itself is released.

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.Activate()
   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

See Also

GetObject Function | Declare Statement | Exception | FileNotFoundException