c# equiv of GetObject and casting

M

mp

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
 
M

mp

mp said:
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
 
A

Arne Vajhøj

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#????

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
 
A

Arne Vajhøj

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.

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
 

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