Creating Reusable Excel Activities Without Keeping Excel Open

I’m having an issue creating library items in Excel using the Invoke VBA activity. The workflow works fine during debugging, but when I drop the activity inside of an Excel Scope, I get this error message after I try to use another activity, such as write range… “The object invoked has disconnected from its clients. (0x80010108 (RPC_E_DISCONNECTED))”

The issue is that I need to put the Invoke VBA inside of a Process/Use Excel File container, and when I exit this container (happens by default when using this as an activity) I disconnect from the process. I tried passing the Excel file variable as an in/out argument but still no luck.

One workaround is to check the “Keep excel file open” in the activity, but this is not ideal. Is there a workaround to allow the Invoke VBA activity to function outside of an Excel Scope?

1 Like

Hi @mahenry
Invoke VBA requires the app scope activity.
Can you check if there is any kind of method like ‘workbook.Close or excelApp.Quit’ that could be enforcing the excel connection to be close?
Also, try ‘Kill Excel’ before ‘Excel Process Scope’.

1 Like

I can’t kill Excel inside the activity since this activity will go inside of another Excel scope. I did try killing Excel before the Excel scope that the activity is dropped into, but I still get the connection error. I think that excelApp.Quit is used by default in the Excel scope which is likely what is causing the issue. I’m not sure how to negate this in a scope activity though.

Are you using nested scope activities in order to have 2 excel opens to perform actions between them?

What I mean is to review if the macro has kind of those methods ‘workbook.Close or excelApp.Quit’.

Another reason, could be that there is an error happening in the macro function that could be raising an exception; you can add exception handling in the macro function and return the value to UiPath; by getting the output of the Invoke VBA activity and then print the value in a log message

'Function to paste an image into an excel sheet
Function FUNCTIONNAME(in_WorkbookName As String, in_SheetName As String)

On Error GoTo ErrorHandler
Application.ScreenUpdating = False
Application.DisplayAlerts = False

    'Code here

ErrorExit:

' Clean up
On Error Resume Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.CutCopyMode = False
Exit Function

ErrorHandler:
’ Hide all error messages and write to a log file instead
FUNCTIONNAME = vbNewLine & “Error: Paste Screenshot on '” & in_SheetName & "'. Description: " & Err.Description
Resume ErrorExit
End Function

@mahenry

Welcome to the community
Looks like inside macro you are closing the excel which might be causing the issue

Cheers