Create COM object / Marshal.GetActiveObject

  • Thread starter Thread starter thomas
  • Start date Start date
T

thomas

I want to get an active Word.Application, via

using System.Runtime.InteropServices;
using Word;
...
object obj = Marshal.GetActiveObject("Word.Application");
_Application wordApp = obj as _Application;

It works fine at my developer computer. At an other computer the application
crash with an exception.
But I can't catch the exception.

Anybody know what is wrong.

Thanks in advance
Thomas
 
Thomas,

Are you trying to catch an Exception (and not a more specific one)? If
you do, you should catch it. My guess is that you are getting an
InvalidCastException. The reason for this would most likely be that your
other computer is running a different version of excel, and therefore, the
interface definitions are different than the version you developed against.

In order to get around this, you would need to make the calls in a late
bound manner, if you want to support multiple versions.

Hope this helps.
 
Hi Nicholas,

1. How can I use in c# late binding?
2. I assume there is security problem.
If I call 'C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\ConfigWizards.exe'
and select the executable as trustable, the exe is running.

Do you know how I can ensure that the assembly is running at every computer
without to use the configWizard?

Cheers Thomas

Nicholas Paldino said:
Thomas,

Are you trying to catch an Exception (and not a more specific one)? If
you do, you should catch it. My guess is that you are getting an
InvalidCastException. The reason for this would most likely be that your
other computer is running a different version of excel, and therefore, the
interface definitions are different than the version you developed
against.

In order to get around this, you would need to make the calls in a late
bound manner, if you want to support multiple versions.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


thomas said:
I want to get an active Word.Application, via

using System.Runtime.InteropServices;
using Word;
...
object obj = Marshal.GetActiveObject("Word.Application");
_Application wordApp = obj as _Application;

It works fine at my developer computer. At an other computer the
application crash with an exception.
But I can't catch the exception.

Anybody know what is wrong.

Thanks in advance
Thomas
 
Back
Top