PC Review


Reply
Thread Tools Rate Thread

c# equiv of GetObject and casting

 
 
mp
Guest
Posts: n/a
 
      15th Jan 2011
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


 
Reply With Quote
 
 
 
 
mp
Guest
Posts: n/a
 
      15th Jan 2011

"mp" <(E-Mail Removed)> wrote in message
news:igqsgv$uin$(E-Mail Removed)...
>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


 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      15th Jan 2011
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.

Arne

 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      15th Jan 2011
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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pentium 4 or equiv AMD vs. Pentium D dual core or equiv gw Windows XP Video 3 4th Aug 2006 05:11 AM
Down-casting of Typed Collection (Casting Generic Types?) conchur Microsoft C# .NET 1 3rd Jul 2006 11:45 AM
Equiv Tramp Freeware 0 14th May 2006 06:49 PM
VB.NET Equiv of... Brian W Microsoft VB .NET 7 12th Apr 2005 06:19 PM
VB.NET Equiv of... Brian W Microsoft C# .NET 7 12th Apr 2005 06:19 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:35 AM.