Avoiding VSTO, Word Automation Timing RPC_E_CALL_REJECTED Errors

0

I am writing a VSTO VB.NET application that controls a Word document, using standard features such as copying, pasting, inserting text, and pages, for example, and no additional threads. I get the following error at different times: Call was rejected by callee. (Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED)).

This appears to be a timing issue with Word, since it fails almost randomly but only on Word calls. In the debugger, I can then click Continue and the code resumes.

I've seen a lot of comments about this, relevant ones seems to be about the COM IMessageFilter interface. But they all skip one important point: I just want to wait for Word to get free again and then continue. I don't want to prompt the user with options (such as with RetryRejectedCall). An ugly work around right now is intentional delays, like Threading.Thread.Sleep(500), but that's hardly the solution, and with many calls that's also impractical.

What is the proper way of handling this?

vb.net
vba
vsto
asked on Stack Overflow Feb 24, 2015 by ForEachLoop

1 Answer

0

Most probably you are using the Word object model from secondary threads. Office application uses the single threaded apartment model. It is not recommended to use any object model from a secondary thread.

Also you may find the similar forum thread helpful.

answered on Stack Overflow Feb 25, 2015 by Eugene Astafiev

User contributions licensed under CC BY-SA 3.0