如何获取活动Excel实例? [英] How to Get Active Excel Instance?

查看:102
本文介绍了如何获取活动Excel实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果只有一个实例运行,它会尝试获取该实例并使用它。这是我正在使用的代码:

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

到目前为止,everythig工作正常,但问题是当我运行多个excel实例时,我想获取Excel的活动实例,但目前我只能获取我打开的第一个实例,这是在运行对象表中注册的第一个实例根据我读的内容。



有没有办法询问一个excel实例,如果它是活动实例,从而使用它?或者如何获得活动实例?

解决方案

有关获取或启动Excel的所有可能性,请参阅此博客文章Andrew Whitechapel:


以编程方式启动Office应用程序


如果您可以获得活动Excel实例的窗口句柄,您可能需要尝试 AccessibleObjectFromWindow ,这将允许您访问OM。


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?

解决方案

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.

这篇关于如何获取活动Excel实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆