I have a form that calls a function in a module that opens a spreadsheet, looks at the value of one cell and returns a variable. Then the code continues and does a similiar thing. I call the function in the Module from several different forms and that is the reason I have it in a module. The code works fine if it is a function in the form. As soon as I move the function to the module and delete the function from the forms, it crashes. I get an error: Automation Error The Remote procedure call failed. And then when that messagebox closes, I get: Error(91) Object variable or With block not set. If I set a breakpoint in the code and step through it, it does not crash. If I set a breakpoint and then continue, it still does not crash. I can make it work with the same code stored in each form that needs it, but it would be nice if I could have one instance of the code in the module.
Any ideas?
Any ideas?
Code:
Dim oXLApp As Object
Dim oXLBook As Object
Dim oXLSheet As Object
isExcelOpen = True
On Error Resume Next
If Err.Number <> 0 Then 'Could not get instance of Excel, so create a new one
isExcelOpen = False
Err.Clear
Set oXLApp = CreateObject("Excel.Application")
End If
On Error GoTo PROC_ERR
oXLApp.Visible = False
Set oXLBook = oXLApp.Workbooks.Open(gAlphaPath)
Set oXLSheet = oXLBook.Sheets(1)
'Do stuff here
oXLApp.Visible = True
oXLBook.Close
If isExcelOpen = False Then
oXLApp.Quit
End If
Set oXLBook = Nothing
Set oXLSheet = Nothing
Set oXLApp = Nothing