Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

c# equiv of GetObject and casting

43 views
Skip to first unread message

mp

unread,
Jan 14, 2011, 8:16:13 PM1/14/11
to
I want to get a reference to a running instance of excel
currently i'm using _xlApp = new Excel.Application();
but obviously that's not what i want

found two answers so far
1 add a reference to the .NET Component "Microsoft.VisualBasic".
and use Microsoft.VisualBasic.Interaction.GetObject()
but that sounded nasty...so i keep searching...

2 System.Runtime.InteropServices.Marshal.GetActiveObject

that sounds better(no vb) so i tried
...
at top of class
private Excel.Application _xlApp;
...
then in method i tried:
_xlApp
=System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
and got this error:
//Cannot implicitly convert type 'object' to 'Excel.Application'.
//An explicit conversion exists (are you missing a cast?)
so i tried
_xlApp
=(Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
and got the same error

I thought (typeToCastTo)objectToCast was the way to cast in c#????

what's the solution?
thanks
mark


mp

unread,
Jan 14, 2011, 8:20:27 PM1/14/11
to

"mp" <nos...@Thanks.com> wrote in message
news:igqsgv$uin$1...@news.eternal-september.org...

>I want to get a reference to a running instance of excel
[]

type 'object' to 'Excel.Application'.
> //An explicit conversion exists (are you missing a cast?)
> so i tried
> _xlApp
> =(Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
> and got the same error

how about that
_xlApp =
(Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel");
seems to work...at least it got rid of the compile error...haven't tested
yet
thanks anyway
mark


Arne Vajhøj

unread,
Jan 14, 2011, 8:21:25 PM1/14/11
to

It is, but the compiler will complain if it is a "side" cast.

_xlApp =
(Excel.Application)(object)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");

will compile.

But will most likely fail at runtime.

Arne

Arne Vajhøj

unread,
Jan 14, 2011, 8:23:23 PM1/14/11
to

Wrong analysis.

It seems already to be an object. So that will not make a difference.

Then the problem probably relates to the fact that this is COM stuff.

Arne

Peter Duniho

unread,
Jan 14, 2011, 9:23:11 PM1/14/11
to

It will depend on the "Excel.Application" type you are using. There are
at least two ways to access the Office application types. You can use
the Office interop wrappers, or you can use COM directly.

I have very little experience with the interop wrappers, so I don't know
how they are implemented. If they are types unto themselves, and
literally _wrapping_ a COM object that is composed within the wrapper
type, then the above line of code won't work.

But, if the interop libraries for Office are simply COM interop
declarations, which take advantage of .NET's COM interop features to
provide a direct one-for-one mapping between the managed object and the
COM object, then you should be able to cast the object as you've shown
above and have it work.

Because of my unfamiliarity with the Office interop stuff, I can't tell
you which is which. But I'd _guess_ they would have done it the latter,
more flexible way. So hopefully what you've tried will work fine. :)

Pete

0 new messages