Access for Microsoft 365 Access 2021 Access 2019 Access 2016 Access 2013 Access 2010 Access 2007 More…Less
Note: The function, method, object, or property described in this topic is disabled if the Microsoft Jet Expression Service is running in sandbox mode, which prevents the evaluation of potentially unsafe expressions. For more information on sandbox mode, search for «sandbox mode» in Help.
Creates and returns a reference to an ActiveX object.
Syntax
CreateObject
(
class
[, servername] )
The CreateObject function syntax has these arguments:
Argument |
Description |
|
Required. Variant (String). The application name and class of the object to create. |
|
Optional. Variant (String). The name of the network server where the object will be created. If servername is an empty string («»), the local computer is used. |
The classargument uses the syntax appname.objecttype and has these parts:
Part |
Description |
|
Required. Variant (String). The name of the application providing the object. |
|
Required. Variant (String). The type or class of object to create. |
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 object, and a Toolbar object.
To create an ActiveX object, assign the object returned by CreateObject to an object variable:
Note: Examples that follow demonstrate the use of this function in a Visual Basic for Applications (VBA) module. For more information about working with VBA, select Developer Reference in the drop-down list next to Search and enter one or more terms in the search box.
' Declare an object variable to hold the object
' reference. Dim as Object causes late binding.
Dim ExcelSheet As Object
Set ExcelSheet = CreateObject("Excel.Sheet")
In this example, we will be automating an Excel spreadsheet object from within an Access database. 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 defined. In the following example, you access properties and methods of the new object using the object variable, ExcelSheet, and other Excel objects, including the Application object and the Cells collection.
' Make Excel visible through the Application object.
ExcelSheet.Application.Visible = True
' Place some text in the first cell of the sheet.
ExcelSheet.Application.Cells(1, 1).Value = "This is column A, row 1"
' Save the sheet to C:test.xls directory.
ExcelSheet.SaveAs "C:TEST.XLS"
' Close Excel with the Quit method on the Application object.
ExcelSheet.Application.Quit
' Release the object variable.
Set ExcelSheet = Nothing
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. 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. For example, you can declare and create the following Excel references:
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.WorkSheet
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
The reference through an early-bound 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 Excel.Application object:
Call MySub (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 Machine Name portion of a share name: for a share named «\MyServerPublic,» 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
Set xlApp = CreateObject("Excel.Application", "MyServer")
Debug.Print xlApp.Version
If the remote server doesn’t exist or 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 Excel. It uses the reference to access the Visible property of Excel, and then uses the Excel Quit method to close it. Finally, the reference itself is released.
Dim xlApp As Object ' Declare variable to hold the reference.
Set xlApp = CreateObject("excel.application")
' You may have to set Visible property to True
' if you want to see the application.
xlApp.Visible = True
' Use xlApp to access Microsoft Excel's
' other objects.
xlApp.Quit ' When you finish, use the Quit method to close
Set xlApp = Nothing ' the application, then release the reference.
Need more help?
Want more options?
Explore subscription benefits, browse training courses, learn how to secure your device, and more.
Communities help you ask and answer questions, give feedback, and hear from experts with rich knowledge.
- Remove From My Forums
-
Question
-
Problem Description: I have several automation projects that I have created and used with Office XP. Now that I have upgraded to Office 2010, none of the automation projects work.
I seems that I no longer have application level objects defined on my system. For instance, the following line in a .vbs file:
Set objExcel = CreateObject(«Excel.Application»)
gives a script error «ActiveX component can’t create object:’Excel.Application'» with error code 800A01AD
When I look in the registry, none of the application entries exist for any of the Office 2010 products.
Operating System: Windows XP 32 bit
Answers
-
Hi exr
Have you installed Office 2010 as «Click-to-run» by any chance?
Cindy Meister, VSTO/Word MVP
-
Marked as answer by
Sunday, October 17, 2010 7:57 PM
-
Marked as answer by
My code, running in MS Access 2010:
Sub test()
Dim xl as Object
set xl = CreateObject("Excel.Application")
End Sub
For whatever reason I am getting the error
ActiveX component can’t create object.
I don’t know why — I go to Tools -> References and I even add the Microsoft Excel library, but still nothing.
asked Oct 26, 2017 at 15:33
user8838318user8838318
211 gold badge1 silver badge3 bronze badges
7
Sometimes creating objects and not releasing them when you’re finished can get things fouled up. Check your Task Manager and see if there are multiple «excel.exe» tasks running. Also note by default when they are created, they do not become visible unless you set the property after instantiating the object variable.
Sometimes rebooting the PC and trying this first will work. If it works after a fresh reboot, then stops working, something’s not getting released properly.
answered Oct 27, 2017 at 14:39
Hi Guys,
How do I register just the excel.dll?
History: I was having the same problems as JN, trying to the do the exact same function (clear shared xls file, and then insert new data) however I resolved the part when it asked for user input to save the file — this occurred because it couldnt save the file as the file was somehow still opened during the initial development stage which locked the file. I simply logged off and logged back on which destroyed any previous connections to that shared file.
So now that the Package is running fine, and not asking for me to save it, I faced my next problem. I could run the package manually from my machine, but when I created a job (DTSRun etc.. under the sql service account on the DBServer) I got the error: ActiveX component can»t create object: »Excel.Application’
Lol, I was stuck on this problem for a while until i happened to stumble upon this thread. So after reading, i tested this package again on a dev machine only to get the same problem. However this time, i knew how to troubleshoot the problem and after installing EXCEL on the dev machine, I was able to run this package automatically on the server under the normal sql service account.
So that comes to me $$$ next question?
How can do I go about registering only the DLL, so that I can create the Excel Object without having to install the full program on the Production machine.
thanks in advance.
‘**************************************************
‘My Code
‘**************************************************
‘**********************************************************************
‘ Visual Basic ActiveX Script
‘************************************************************************
Function Main()
Dim xlsApp
Dim xlsWorkBook
Dim xlsSheet
Dim NumRows
Dim LastRow
Set xlsApp = CreateObject(«Excel.Application»)
Set xlsWorkBook = xlsApp.Workbooks.Open(«\fileserverreportsMyReport.xls»)
LastRow = 1000 ‘arbitrary definition
‘******MySheet1 Sheet
Set xlsSheet = xlsWorkBook.Sheets(«MySheet1»)
NumRows = xlsSheet.UsedRange.Row — 1 + xlsSheet.UsedRange.Rows.Count
xlsSheet.Range(«A2:H» & LastRow).Clear
xlsSheet.Range(«A2:H» & NumRows).Delete
‘Close the workbook saving changes.
Set xlsSheet = Nothing
xlsWorkBook.Close True
Set xlsWorkBook = Nothing
xlsApp.Quit
Set xlsApp = Nothing
Main = DTSTaskExecResult_Success
End Function
|
|