Error Executing Macro. Unable to cast COM

Scenario: Trying to execute a macro in a excel aplication scope. To execute this macro i need to have open another 2 excel files. Doing this manual it works just fine. Note that it takes a while to finish running the macro (in manual execution).

Steps to reproduce: You need my files to reproduce. Using 3 excel files in excel application scopes and execute macro.

Current Behavior: Macro runs and after some time it throw’s the following error: Execute macro : Unable to cast COM object of type ‘System.__ComObject’ to interface type ‘Microsoft.Office.Interop.Excel._Workbook’. This operation failed because the QueryInterface call on the COM component for the interface with IID ‘{000208DA-0000-0000-C000-000000000046}’ failed due to the following error: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD)).

Expected Behavior: To execute macro and wait untill it is finished. (it takes a long time to execute due to large data inside excel file).

Studio/Robot/Orchestrator Version: 2016.6442

Last stable behavior: no stable behavior
Last stable version: 2016.6442
OS Version: Windows 7 Enterprise
Others if Relevant: (workflow, logs, .net version, service pack, etc):I have this problem with another macro that takes some time to execute. I get the same error message.

I have posted in another topic and no one helped me. I even talked to a UiPath employee and couldn’t give me a solution.

1 Like

Hey,

You should check if the macro has the following instruction in it:
Application.Quit

I have had a similar issue with a macro execution activity, and after a thorough inspection as to why the exception is thrown, I found out that if I delete the previously mentioned code line from the macro, the activity works flawless.

Try removing it or finding a work-around if you really need that line.
Hope this helps!

@danielcraciun

i am Facing same error what you faced earlier while running macro.
Sometimes macro working fine. Sometimes throws an error.
Please suggest me how to solve this.

image

Hi, I couldn’t find any fixes. I still get HRESULT exceptions sometimes, you could close or even kill Excel process, maybe add a delay before the macro executes and after the Execute Macro, in the Excel App Scope you could use Close Workbook, where you should add as input the existing ExcelWorkbookScope variable.

Regards,
Daniel

@danielcraciun I have seen you used checkempty, kindly explain purpose of using checkempty after macro execution…

Hi danielcraciun,

How about to open those excels by Macro?

I am using the checkEmpty to check a condition in the process.

I think you could use instead of execute macro some activities like: send hotkeys, clicks and on element appear to run a macro. You could open the excel file (using run command or by excel app scope with visible option checked), then send hotkey (altf+f8) to open macro window from where you could double click to run a macro. To know when your macro has finished executing you can add a message box in the macro(sub) and use on element appear to click the button from the message box to end the macro execution.

To avoid exceptions you should modify the TimeoutMS property of On Element Appear so the time would be enough for the message box of the macro to appear.