如何使用进程ID获取Excel实例或Excel实例CLSID? [英] How to get Excel instance or Excel instance CLSID using the Process ID?

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

问题描述

我正在使用C#,我需要通过进程ID获取一个特定的excel实例;我从另一个应用程序获取我需要的实例的进程ID,但是我不知道还有什么要做,我不知道如何获得运行的excel的实例,给定他的进程ID。



我已经在网上进行了很多研究,但是我只看到使用Marshal.GetActiveObject(...)或Marshal.BindToMoniker(...)的例子,我不能使用因为第一个返回在ROT中注册的第一个Excel实例,而不是精确的我需要的Excel实例,第二个Excel实例要求您在尝试获取实例之前保存excel文件。



此外,如果我能够获取我需要的excel实例的CLSID,使用进程ID,那么我可以调用

  GetActiveObject(ref _guid,_ptr,out objApp);  

最终将返回

解决方案

通过进程ID识别进程,可以获取 Process.MainWindowHandle ,然后使用它与 AccessibleObjectFromWindow API 以访问该进程的Excel对象模型。



文章将应用程序对象置于一个Shimming自动化加载项中由Andrew Whitechapel详细介绍了这一技术,以及示例代码。



那篇文章开始于以下行:

  int hwnd =(int)Process.GetCurrentProcess()。MainWindowHandle 

在您的情况下,您可能会看起来更像:

  int excelId = 1234; //适当改变! 
int hwnd =(int)Process.GetProcessById(excelId).MainWindowHandle

excelId是您要查找的进程标识号。否则,代码应该与本文中给出的基本相同。 (忽略他的代码为加载项编写的事实;这方面不会影响您的需求,所以只需忽略它。)



如果你不具有进程标识,那么您将需要使用进程.GetProcessesByName ,您可以根据需要枚举每一个并抓取每个Excel实例的访问对象模型。



希望这有帮助,



Mike


I'm working with C#, I need to obtain a specific instance of excel by it's process ID; I get the Process ID of the instance that I need from another application but I don't know what else to do, I don't know how can I get a running instance of excel given his process ID.

I have researched a lot on the web, but I have only see examples of using Marshal.GetActiveObject(...) or Marshal.BindToMoniker(...), which I can't use since the first one returns the first Excel instance registered in the ROT and not precisely the one that I need, and the second one requires that you save the excel file before trying to get the instance.

Also, if I where able to get the CLSID of the excel instance that I need, using the process ID, then I may be able to call

GetActiveObject(ref _guid, _ptr, out objApp);

that ultimately will return the excel instance that I need.

解决方案

Once you identify the process via the process id, you can get the Process.MainWindowHandle and then use that along with the AccessibleObjectFromWindow API to get access to the Excel object model for that process.

The article Getting the Application Object in a Shimmed Automation Add-in by Andrew Whitechapel describes this technique in detail, along with sample code.

The key code in that article for you begins at the line:

int hwnd = (int)Process.GetCurrentProcess().MainWindowHandle

Which in your case might look more like:

int excelId = 1234; // Change as appropriate!
int hwnd = (int)Process.GetProcessById(excelId).MainWindowHandle

where the 'excelId' is the process id number that you are looking for. Otherwise, the code should be essentially the same as given in the article. (Ignore the fact that his code is written for an add-in; that aspect won't affect your needs here, so just ignore it.)

If you do not have the process id, then you you would want to use Process.GetProcessesByName, whereby you could enumerate each one and grab control of each Excel instance with access to the object model, as needed.

Hope this helps,

Mike

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

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