Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

DISP_E_EXCEPTION When calling VBA macro from background thread in C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Omatase - 06 Sep 2007 02:55 GMT
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
Nicholas Paldino [.NET/C# MVP] - 06 Sep 2007 16:11 GMT
Omatase,

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

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> 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
[quoted text clipped - 31 lines]
>
> Thanks
Omatase - 06 Sep 2007 17:41 GMT
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?
Nicholas Paldino [.NET/C# MVP] - 06 Sep 2007 19:34 GMT
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?

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> No I did not.
>
[quoted text clipped - 12 lines]
> anyone know why this would happen? Does it have to do with the COM
> apartment state?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.