Attach to existing Excel Instance

穿精又带淫゛_ 提交于 2019-12-05 13:45:55

Marshal.GetActiveObject() doesn't take a process ID as a parameter. What you want is:

Marshal.GetActiveObject("Excel.Application");

Note that this doesn't require keeping track of the process at all, there just needs to be one.

It gets a lot more complicated if you can have multiple processes and want to attach to a specific one. That is where the answer to the other question comes in.

There is also a good article at http://blogs.msdn.com/b/andreww/archive/2008/11/30/starting-or-connecting-to-office-apps.aspx with a more full description of different ways of launching excel. Note that not all of them are necessarily up to date with Excel 2013, where having a single process for all Excel windows complicates things. For your purposes though, the GetActiveObject solution should be fine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!