DISP_E_EXCEPTION When calling VBA macro from background thread in C#

O

Omatase

This may actually be a COM or VBA issue but I'm hoping it can be
answered here anyway. I am making a call to a VBA macro in a Word
document in a background thread in my C# application. The call
executes fine on my machine. However, on the tester machines they get
the following error:

System.Reflection.TargetInvocationException: Exception has been thrown
by the target of an invocation. --->
System.Runtime.InteropServices.COMException (0x80020009): Exception
occurred. (Exception from HRESULT: 0x80020009 (DISP_E_EXCEPTION))
--- End of inner exception stack trace ---
at System.RuntimeType.InvokeDispMethod(String name, BindingFlags
invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers,
Int32 culture, String[] namedParameters)
at System.RuntimeType.InvokeMember(String name, BindingFlags
bindingFlags, Binder binder, Object target, Object[] providedArgs,
ParameterModifier[] modifiers, CultureInfo culture, String[]
namedParams)
at
BGM.Gorilla.PresentationLayer.Common.MessageSender.RunMacro(String
macroName, Object[] list) in E:\Code\Gorilla3\Code\Gorilla3managed
\PresentationLayer\Common\MessageSender.cs:line 162
at
BGM.Gorilla.PresentationLayer.Common.EmailSender.initiateEmailBatching(Object
isReprint) in E:\Code\Gorilla3\Code\Gorilla3managed\PresentationLayer
\Common\EmailSender.cs:line 140

The error does not occur on any particular line of code (at least not
one I can see while debugging). But it is caught by the C#
application. I am executing the macro call in a try catch block and
the error is trapped there.

Please help,

Thanks
 
N

Nicholas Paldino [.NET/C# MVP]

Omatase,

In the background thread, have you called SetApartmentState on the
thread, to initialize the COM apartment state?
 
O

Omatase

No I did not.

Additional information:

In the C# app in the background thread before I call the VBA macro I
open a word template file in the Word ApplicationClass. After the
macro execution completes I close all open documents and then call
Quit on the word application class instance. I have discovered that
there is a line in the VBA macro code that explicitly closes the
template file I am opening in the C# code. This code executes fine on
my machine but causes the above error on tester machines. When I
removed the line in the VBA code that closes this file and just let
the C# code handle the closing of the file it executes without error
on the tester machines. This is a suitable work around for me but does
anyone know why this would happen? Does it have to do with the COM
apartment state?
 
N

Nicholas Paldino [.NET/C# MVP]

Omatase,

You should be setting the apartment state. I would set it to STA, and
then see if you get the bug. Strange errors can occur if you do not set the
apartment state correctly when using COM.

If you still get the error, then can you give more information about
what you are doing with the template file?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top