ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Calling Excel methods from a different thread (COM add-in) (https://www.excelbanter.com/excel-programming/416924-calling-excel-methods-different-thread-com-add.html)

[email protected]

Calling Excel methods from a different thread (COM add-in)
 
Hello all. I need to know if there is any way to this. I am using VS/
C# and COM interop to develop an Add-in for Excel.

I have a thread that tries to write on some cells of the current
spreadsheet. The problem is that when Excel is busy, these call return
an exception.


class MyClass {

Excel.Range cell;

public MyClass(Excel.Range cell) { this.cell = cell; }

// This method is called from a different thread than the original one
where the add-in starts
void AsynchronousWrite(object stuff)
{
cell.Value2 = stuff;
}
}

That throws the following exception when Excel is busy (for example
when the user is editing the content of a cell):

System.Runtime.InteropServices.COMException (0x800A03EC): Exception
from HRESULT: 0x800A03EC

Is there any straightforward way to solve this problem?

Thank you for your help.


Peter T

Calling Excel methods from a different thread (COM add-in)
 
In later versions you can check the app.Ready state, loop while false or
with a timer. Otherwise if you are sure there is no reason for the error, or
rather you get that particular error, similarly defer.

Perhaps after some determined time SendKeys (or API equivalent) an Esc in
case user has accidentally left Excel in edit mode (ensure Excel is the
active window)

Regards,
Peter T

wrote in message
...
Hello all. I need to know if there is any way to this. I am using VS/
C# and COM interop to develop an Add-in for Excel.

I have a thread that tries to write on some cells of the current
spreadsheet. The problem is that when Excel is busy, these call return
an exception.


class MyClass {

Excel.Range cell;

public MyClass(Excel.Range cell) { this.cell = cell; }

// This method is called from a different thread than the original one
where the add-in starts
void AsynchronousWrite(object stuff)
{
cell.Value2 = stuff;
}
}

That throws the following exception when Excel is busy (for example
when the user is editing the content of a cell):

System.Runtime.InteropServices.COMException (0x800A03EC): Exception
from HRESULT: 0x800A03EC

Is there any straightforward way to solve this problem?

Thank you for your help.





All times are GMT +1. The time now is 01:13 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com