Here is a VBA code routine that more thoroughly attempts to either Get or Create an instance of the Excel application, while avoiding potential errors, for use when you have your Reference turned on for the Excel Object Library. This can be used as it is within your main code, or put into a Function of its own. It also includes a Boolean variable that is meant to record whether or not Excel was already open when the routine started, which you can use at the end to judge whether to close the Excel application when your done with it.
I put in a bunch of comment lines to help explain what's going on, for those not as familiar with VBA or this process.
Dim oExcel As Excel.Application
Dim oXLWasOpen As Boolean 'False by default
On Error Resume Next
'try to get open instance of Excel Application
Set oExcel = GetObject(, "Excel.Application") 'tries to retrieve running instance of Excel
If Err = 0 Then
'it found and retrieved an open instance of Excel
oXLWasOpen = True 'so I know whether to close Excel at the end
On Error GoTo 0 'resets error handling to normal
ElseIf Err <> 0 Then
'it failed to get an open instance of Excel, so now create one
Err.Clear 'clear it so we know if another error happens
'try to create an instance of Excel (Open Excel)
Set oExcel = CreateObject("Excel.Application")
If Err <> 0 Then
'the attempt to create an instance of Excel failed too
Call MsgBox("Failed to Get or Create Excel Application instance. Exiting.", , "")
Exit Sub
Else
'it succeeded to create an instance of Excel (opened Excel)
Err.Clear
On Error GoTo 0 'resets error handling to normal
End If
End
If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.
If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡or you can Explore My CONTRIBUTIONS
Wesley Crihfield

(Not an Autodesk Employee)