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