On 14-01-2011 20:21, Arne Vajhøj wrote:
> On 14-01-2011 20:16, mp wrote:
>> 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.
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
|