Share via

VBA CreateObject won't create Excel application object

Anonymous
2016-05-25T18:45:54+00:00

We have Office 2010.  We have several Access 2010 databases that run VBA code that among other things, calls a routine to export data to an Excel spreadsheet.  These have been working well for several years.  We have several people run these databases locally on their Windows 7 machines.  Lately, we have 2 users that suddenly are having problems with this export routine.  None of the VB code has changed and the other users can still run it successfully.  The line of code that is generating the 424 - Object Required error is in bold below.  Its the **Set objExcel = CreateObject("Excel.Application")**line.  The CreateObject function is not creating the Excel application object.  Excel is on the user's machine and he can launch it manually and has no problems with it.  These issues started about a month ago and we cannot determine the cause.  We assume its some sort of corruption with a DLL or something in amiss in the registry.  Like I said, he ran this successfully for almost 2 years before it starting having problems and nothing has changed in the code.  As you can see, we are using late-binding.  We have also converted this to early-binding (using the MS Excel 14.0 Object Library and MS Office 14.0 Object Library) as references and have verified that he has these on his machine.

This is not a Access problem but an Excel problem.  I have also created a Word document containing a macro that attempts to create the Excel.Application object using the CreateObject command and it generates the same error.  I have even created VBScript files that do the same thing and it generates the same error.  I also created VBScript files that create a Word.Application object, a Powerpoint.Application object and they work successfully.  Its only the Excel.Application object creation that is failing.

Our IT department has run repairs on Office which did not correct the error and they have uninstalled and reinstalled Office and that also did not fix the problem.  We even had them give one user a new machine and this solved the problem for about 3 weeks and then he started getting the error again.

Public Function ExcelExport(ByVal strfilename As String, ByVal strWKSheet As String, ByVal strMyRange As String, ByVal strDBName As String, _

                            ByVal strMyQuery As String, ByVal blnIsCopyFile As Boolean, ByVal blnHeaderRow As Boolean, _

                            ByVal strOldRange As String, Optional strPara1 As String = "", Optional strPara2 As String = "", _

                            Optional strrow As String, Optional strcellvalue As String) As Boolean

    'Purpose:   Exports data from an Access query to an Excel spreadsheet

    'Accepts:   strfilename - the name of the Excel spreadsheet file to import the data into

    '           strWKSheet - the name of the worksheet to import the data into

    '           strMyRange - the first cell where the data will be written

    '           strDBName - the filename of the Access database used for exporting the data

    '           strMyQuery - the Access query that generates the data to export

    '           blnIsCopyFile - not used

    '           blnHeaderRow -

    '           strOldRange - an Excel range that will be cleared out

    '           strPara1 - a parameter value to pass to the Access query

    '           strPara2 - a parameter value to pass to the Access query

    '           strrow - an Excel range that will be set to "cellvalue"

    '           strcellvalue - a value that will be set in the "row" parameter Excel range

    'Returns:   True if no error, False otherwise

    Dim lngColumn As Long

    Dim objExcel As Object

    Dim objWorkbook As Object

    Dim objWorksheet As Object

    Dim objCell As Object

    Dim dbs As DAO.Database

    Dim rst As DAO.Recordset

    DoCmd.Hourglass True

    On Error GoTo ErrorHandler

    ExcelExport = False

    If IsFileOpen(strfilename) Then

        'msgbox buttons system modal, ok

        MsgBox "File - '" & strfilename & "' is open. Please close it and try again.", vbSystemModal

        GoTo ErrorHandler

    End If

    Set objExcel = CreateObject("Excel.Application")

    ' Change True to False if you do not want the workbook to be visible when the code is running

    objExcel.Visible = False

    ' actual path and filename of the EXCEL file into which you will write the data

    Set objWorkbook = objExcel.Workbooks.Open(strfilename)

    ' Replace WorksheetName with the actual name of the worksheet in the EXCEL file (note that the worksheet must already be in the EXCEL file)

    Set objWorksheet = objWorkbook.Worksheets(strWKSheet)

    If strOldRange <> "" Then

        Set objCell = objWorksheet.Range(strOldRange)

        objWorksheet.Select

        objCell.Select

        'objCell(Selection, Selection.End(xlDown)).Select

        objCell.ClearContents

    End If

    ' Replace A1 with the cell reference into which the first data value is to be written

    Set objCell = objWorksheet.Range(strMyRange) ' this is the first cell into which data go

    If strDBName = "CurrentDb" Then

        Set dbs = CurrentDb

    Else

        Set dbs = Workspaces(0).OpenDatabase(strDBName)

    End If

    If strrow <> "" Then objWorksheet.Range(strrow).Value = strcellvalue

    ' name of the table or query whose data are to be written into the worksheet

    Set rst = RunSelectQry(strDBName, strMyQuery, strPara1, strPara2, rst, dbs)

    If rst.EOF = False And rst.BOF = False Then

        rst.MoveFirst

        If blnHeaderRow = True Then

            'write field names as the header row

            For lngColumn = 0 To rst.Fields.Count - 1

                objCell.Offset(0, lngColumn).Value = rst.Fields(lngColumn).Value

            Next lngColumn

            Set objCell = objCell.Offset(1, 0)

        End If

        objWorksheet.Range(strMyRange).CopyFromRecordset rst

    End If

    ExcelExport = True

ErrorHandler:

    DoCmd.Hourglass False

    ' Close the EXCEL file while saving the file, and clean up the EXCEL objects

    If Not (objCell Is Nothing) Then Set objCell = Nothing

    If Not (objWorksheet Is Nothing) Then Set objWorksheet = Nothing

    If Not (objWorkbook Is Nothing) Then

        objWorkbook.Close True ' close the EXCEL file and save the new data

        Set objWorkbook = Nothing

    End If

    If Not (objExcel Is Nothing) Then

        objExcel.Quit

        Set objExcel = Nothing

    End If

    If Not rst Is Nothing Then

        rst.Close

        Set rst = Nothing

    End If

    If Not dbs Is Nothing Then

        dbs.Close

        Set dbs = Nothing

    End If

    If Err.Number <> 0 Then

        If Err.Number = 9 Then

            If Err.Description = "Subscript out of range" Then

                'msgbox buttons system modal, ok

                MsgBox "Worksheet '" & strWKSheet & "' does not exist in file - " & strfilename, vbSystemModal, "ExcelExport - " & strMyQuery

            Else

                'msgbox buttons system modal, ok

                MsgBox Err.Number & " " & Err.Description, vbSystemModal, "ExcelExport - " & strMyQuery

            End If

        Else

            'msgbox buttons system modal, ok

            MsgBox Err.Number & " " & Err.Description, vbSystemModal, "ExcelExport - " & strMyQuery

        End If

    End If

End Function

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

8 answers

Sort by: Most helpful
  1. Anonymous
    2016-05-26T17:04:26+00:00

    Would it be worth trying to re-register Excel.exe?  The user can manually launch Excel with no problem but I read that sometimes the registry gets messed up and you need to reregister an application.  The user can currently manually launch Excel without any problems and he can dbl-click on an Excel file and it will launch Excel and open the file.

    What I read was to type in the command prompt:

    excelapplicationpath\Excel.exe /regserver

    our path to Excel.exe is: C:\program files (x86)\Microsoft Office\Office14\Excel.exe

    Do you think it would be helpful to try this or could it further screw something up?

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2016-05-26T11:48:09+00:00

    I don't understand what you are saying.  What does that have to do with the CreateObject("Excel.Application") which is where the error occurs.  The CreateObject command is not returning the Excel application object.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  3. Paul Edstein 82,861 Reputation points Volunteer Moderator
    2016-05-26T00:15:00+00:00

    Have you tried repairing the Office installation (via Start > Windows Control Panel > Programs > Programs & Features > Microsoft Office (version) > Change > Repair)?

    PS: You only need to post once...

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  4. Paul Edstein 82,861 Reputation points Volunteer Moderator
    2016-05-26T11:39:14+00:00

    It's possible the document/template has acquired some of corruption. Corrupt documents/templates can often be 'repaired' by inserting a new, empty, paragraph at the very end, copying everything except that new paragraph to a new document/template (headers & footers may need to be copied separately), closing the old document/template and saving the new one over it.

    Similarly, if the document/template has tables, it's possible it has a corrupt one. Corrupt tables can often be 'repaired' by converting the tables to text and back again or by saving the document/template in RTF format, closing the document/template then re-opening it and re-saving in the doc/t(x/m) format, as appropriate.

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2016-05-26T10:37:01+00:00

    Yes.  I stated that our IT dept ran the repairs on Office and it didn't work and then they uninstalled/reinstalled Office and that did not work either.

    Was this answer helpful?

    0 comments No comments