Hi – I have code which works fine in Excel but when I try it from Access i get an error “ActiveX component cannot create object”.
In Access I have a project reference set to Excel and I’m using early binding. All I want to do is transfer the results of a query to a worksheet and then open the sheet in Excel and set a range object.
Here is the code:
Private Sub butOpenInExcel_Click()
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim strFilePath As String
Dim fso As FileSystemObject
Dim rngStartCell As Range
Set xlApp = CreateObject(“Excel.Application”)
strFilePath = xlApp.DefaultFilePath & “Calls.xls”
Set fso = CreateObject(“Scripting.FileSystemObject”)
With fso
If .FileExists(strFilePath) Then
.DeleteFile (strFilePath)
End If
End With
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, “qryCustomReport”, strFilePath, True
Set xlApp = CreateObject(“Excel.Application”)
xlApp.Visible = True
strFilePath = xlApp.DefaultFilePath & “Calls.xls”
xlApp.Workbooks.Open strFilePath
Set xlBook = xlApp.ActiveWorkbook
Set xlSheet = xlBook.ActiveSheet
‘NO PROBLEM THIS FAR, BUT NOW I CAN’T SET A RANGE
‘I”VE TRIED LOTS OF CODE BUT NOTHING WORKS, E.G.
xlsheet.Range(“A1”).Select
set rngStartCell = selection ‘ ERROR “ActiveX component cannot create object”.
‘ OR
set rngStartCell = xlsheet.Cells(1,1) ‘ERROR “Application defined or object-defined error”
End Sub
Any idea greatly appreciated!