Can create object excel application

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


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 computer is used.

The classargument uses the syntax appname.objecttype and has these parts:

Part

Description


appname

Required. Variant (String). The name of the application providing the object.


objecttype

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

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

user8838318's user avatar

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

Gordon Prince's user avatar

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

  • Home
  • VBForums
  • Visual Basic
  • ASP, VB Script
  • VBScript runtime error: ActiveX component can’t create object: ‘Excel.Application’

  1. Sep 5th, 2014, 02:05 PM


    #1

    krcruz is offline

    Thread Starter


    New Member


    VBScript runtime error: ActiveX component can’t create object: ‘Excel.Application’

    I am attempting to write a script for an application called Syspro that uses VBScript as a means to extend the functionality of the product. The script I’m working on is supposed to take a report generated by the program and output it into an excel spreadsheet. I am attempting to test code I’ve written so far to confirm functionality and am running into problems trying to create excel spreadsheets. I suspect I may have some permissions issue but do not exactly know where to look or how to check without trying to hamfist it by loosening all of my security settings.

    I am testing this code in two different environments: as a web application in Visual Studio 2013 express, and as a script itself within the Syspro application. In both instances, my code takes an XML file and appropriately parses the data into a single multi-dimensional array, which is then passed into a function that creates the spreadsheet. The function that is not behaving properly I’ve reproduced below:

    Code:

    Function createSheets(sArray)
    	Dim dateToday, strFileName
            Dim objExcel
    	Dim i, j
    
    	dateToday = Month(Date) & "-" & Day(Date) & "-" & Year(Date)
    	strFileName = "C:XML-AVGPRC-" & dateToday & ".xlsx"
        
        Set objExcel = CreateObject("Excel.Application")
        objExcel.Visible = True
    	objExcel.DisplayAlerts = FALSE
    	objExcel.workbooks.Add
    	
        For i = 0 To UBound(sArray)
            For j = 0 To UBound(sArray(i))
                objExcel.Cells(i+1,j+1).Value = sArray(i)(j)
            Next
        Next
    	
        objExcel.ActiveWorkbook.SaveAs(strFileName)
        objExcel.Quit
    
    End Function

    When I run this code in the VS2013 debugger, the script fails on «Set objExcel = CreateObject(«Excel.Application»)» and provides the error message in this post’s subject line.

    When I run this code in Syspro, the script actually completes; an excel file is created, properly named, and saved in the correct location. However the file is empty, containing no data.

    I’m new to VBScripting, my development background is ColdFusion so I’m sure there’s a fair amount of things I’m not accounting for here. All assistance is appreciated.


  2. Sep 6th, 2014, 03:48 AM


    #2

    Re: VBScript runtime error: ActiveX component can’t create object: ‘Excel.Application

    the machine where you get the «ActiveX component can’t create object» error most likely does not have excel installed. You need Excel to be installed for this.

    to resolve the empty cells issue i guess this change would help:

    Code:

        For i = 0 To UBound(sArray)
            For j = 0 To UBound(sArray(i))
                objExcel.ActiveWorkbook.Sheets(1).Cells(i+1,j+1).Value = sArray(i)(j)
            Next
        Next


  3. Sep 8th, 2014, 08:15 AM


    #3

    krcruz is offline

    Thread Starter


    New Member


    Re: VBScript runtime error: ActiveX component can’t create object: ‘Excel.Application

    Quote Originally Posted by digitalShaman
    View Post

    the machine where you get the «ActiveX component can’t create object» error most likely does not have excel installed. You need Excel to be installed for this.

    to resolve the empty cells issue i guess this change would help:

    Code:

        For i = 0 To UBound(sArray)
            For j = 0 To UBound(sArray(i))
                objExcel.ActiveWorkbook.Sheets(1).Cells(i+1,j+1).Value = sArray(i)(j)
            Next
        Next

    I’m replying right now to acknowledge that I’ve seen this and will give it a shot. I can tell you that both instances where I am trying to run the code are on the same machine; Excel is installed. I think it might have something more to do with a permissions issue with an account or folder, but I’m not sure which. I’ll reply back after I’ve tried this (and a few others if it doesn’t work as I’m polling multiple forums) with results and/or solution.


  4. Sep 8th, 2014, 09:39 AM


    #4

    krcruz is offline

    Thread Starter


    New Member


    Re: VBScript runtime error: ActiveX component can’t create object: ‘Excel.Application

    Hi digitalshaman,

    The change you suggested did not resolve the problem, unfortunately nothing has changed. Is there more information I can provide to you that can help resolve this? My dev environment is Windows 7 Pro with Excel 2013.


  5. Sep 8th, 2014, 02:21 PM


    #5

    Re: VBScript runtime error: ActiveX component can’t create object: ‘Excel.Application

    Quote Originally Posted by krcruz
    View Post

    Hi digitalshaman,

    The change you suggested did not resolve the problem, unfortunately nothing has changed.

    ah, i see. you are using the multidimensional array incorrectly.
    this:

    Code:

    For j = 0 To UBound(sArray(i))

    would need to read

    Code:

    For j = 0 To UBound(sArray, 2)

    and this:

    Code:

    objExcel.Cells(i+1,j+1).Value = sArray(i)(j)

    needs to be

    Code:

    ws.Cells(i + 1, j + 1).Value = sArray(i, j)

    i think you have an «on error resume next» somewhere and thats why it silently saved an empty workbook instead of throwing an error.

    i have used the following code in excel vba to test the function:

    Code:

    Option Explicit
    
    Sub a()
        ReDim sArray(5, 10)
        Dim i, j
        For i = 0 To 5
          For j = 0 To 10
            sArray(i, j) = i & "," & j
          Next
        Next
        createSheets (sArray)
    End Sub
    
    Function createSheets(sArray)
        Dim dateToday, strFileName
        Dim objExcel
        Dim i, j
        Dim wb, ws
    
        dateToday = Month(Date) & "-" & Day(Date) & "-" & Year(Date)
        strFileName = "C:XML-AVGPRC-" & dateToday & ".xlsx"
        
        Set objExcel = CreateObject("Excel.Application")
        objExcel.Visible = True
        objExcel.DisplayAlerts = False
        
        Set wb = objExcel.Workbooks.Add
        Set ws = wb.Sheets(1)
        
        For i = 0 To UBound(sArray)
            For j = 0 To UBound(sArray, 2)
                ws.Cells(i + 1, j + 1).Value = sArray(i, j)
            Next
        Next
        
        wb.SaveAs (strFileName)
        objExcel.Quit
    
    End Function

    in regards to web applications in VS, i have never done any but it would be logical that excel.application is not available in a web application.


  6. Sep 9th, 2014, 12:04 PM


    #6

    krcruz is offline

    Thread Starter


    New Member


    Re: VBScript runtime error: ActiveX component can’t create object: ‘Excel.Application

    digitalShaman, your suggestion has definitely resulted in a change in function. I’ve begun encountering new errors in unrelated parts of my code after implementing this, which leads me to believe that my incorrect looping was causing the application to fail silently in an error catch. I confirmed that I can now output to a spreadsheet with a modified version of my array creation code that uses hard-coded values. I’m debugging these new errors and will update once I have a new scenario. Thank you for your help


  7. Dec 6th, 2015, 12:25 PM


    #7

    VBExplorer12 is offline


    Member


    Re: VBScript runtime error: ActiveX component can’t create object: ‘Excel.Application

    Quote Originally Posted by krcruz
    View Post

    I’m replying right now to acknowledge that I’ve seen this and will give it a shot. I can tell you that both instances where I am trying to run the code are on the same machine; Excel is installed. I think it might have something more to do with a permissions issue with an account or folder, but I’m not sure which. I’ll reply back after I’ve tried this (and a few others if it doesn’t work as I’m polling multiple forums) with results and/or solution.

    krcruz,

    Are you still around? How did you resolve the ActiveX component error? I have Office 2013, Windows 8.1, and receive the same error, when running a VBScript.

    The same VBScript will run fine on a Windows 8.1 Office 2013 laptop, and 2 Windows 7 Office 2010 PCs.


  • Home
  • VBForums
  • Visual Basic
  • ASP, VB Script
  • VBScript runtime error: ActiveX component can’t create object: ‘Excel.Application’


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
  • BB code is On
  • Smilies are On
  • [IMG] code is On
  • [VIDEO] code is On
  • HTML code is Off

Forum Rules


Click Here to Expand Forum to Full Width

Понравилась статья? Поделить с друзьями:
  • Calculating percentages with excel
  • Can columns be sorted in excel
  • Calculating month in excel
  • Can click button in excel
  • Calculating formulas for excel