I am writing some automation code to populate an Excel Template (.XLT) file with data retrieved from an Access query. All goes well until I try to run the process a second time. I am getting hammered with RPC Automation errors. This seems to go away if I manually go into the code module and choose Run>Reset, and make sure there is no hidden instance of Excel hanging around.
What I am looking for:
1. How can I make absolutely sure that there is no instance of Excel left open when I exit my function (Whether or not an error occurs)?
( The Task manager only shows it in the Processes pane, not the Applications pane.)
2. Is there any way to perform the Run>Reset command within my VBA code? (not that I should have to…)
3. Has anyone found anything more useful on the topic of RPC Automation Error than what I find on the MSKB site?
I wait for your ideas with baited breath!!! Thanks so much…
Rich P
Here is some of the pertinent code…
Dim xlAPP As Excel.Application, xlWB As Workbook
Dim ExcelWasNotRunning As Boolean
On Error Resume Next
ExcelWasNotRunning = False
Set xlAPP = GetObject(, “Excel.Application”)
If Err.Number 0 Then
ExcelWasNotRunning = True
Err.Clear
Call DetectExcel
Set xlAPP = CreateObject(“Excel.Application”)
End If
On Error GoTo ErrorHandler
‘ Add a workbook based on the specified template
Set xlWB = xlAPP.Workbooks.Add(strTemplateName) ‘Open a new file based on a template
do my thing here…
then clean up…
xlAPP.Visible = True
xlAPP.UserControl = False
xlWB.Windows(1).Visible = True
xlWB.SaveAs strSaveAsName
‘If we opened Excel then Quit it
‘If ExcelWasNotRunning = True Then
xlAPP.Application.Quit
‘End If
‘
IN_Make_Invoice_Summary_Exit:
rst.Close
Set qdf = Nothing
Set db = Nothing
Set xlWB = Nothing
Set xlAPP = Nothing ‘ Clean up on the way out
Exit Function
ErrorHandler:
MsgBox “An error occurred while trying to build the Invoice Summary.” & vbCrLf _
& “The specific error number and description are: ” & vbCrLf _
& Err.Number & vbCrLf & Err.Description & vbCrLf _
& “Please wait a moment and then try again. If the problem persists, ” _
& “contact your database administrator with this error info.”
GoTo IN_Make_Invoice_Summary_Exit