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