tags:

views:

603

answers:

1

I have an application in C#, this application paste info to excel on user demand, the logic behind is like this, if there are none excel instances running, it creates one and paste to that instance.

If there is only one instance running, it tries to get that instance and work with it. This is the code I'm using to do so:

Guid _guid;
CLSIDFromProgID("Excel.Application", out _guid);
if (_guid == Guid.Empty) {...}
IntPtr _ptr = new IntPtr();
GetActiveObject(ref _guid, _ptr, out objApp);

So far everythig works fine, but the problem that I have is when there are more than one excel instances running, I want to get the active instance of Excel, but currently I'm only able to get the first instance that I opened, which is the first instance registered in the Running Object Table according to what I read.

Is there a way to "ask" to an excel instance if it is the active instance and thus use it? or how can I get the active instance?

+1  A: 

For all possibilities to get hold of or to start Excel see this blog post by Andrew Whitechapel:

Launching Office Apps Programmatically

If you can get a window handle of the "active" Excel instance you might want to try AccessibleObjectFromWindow which will give you access to the OM.

0xA3