如何使用后期绑定访问 Microsoft Word 现有实例
我正在用 C# 开发一些代码,我将在其中与 Microsoft Word 进行交互。我希望能够选择重新使用现有实例或作为创建新实例的替代方案。
请记住,我想使用 LATE BINDING 来完成所有这些...可以肯定地说,我已经弄清楚如何在创建新实例时让事情正常工作..我只是调用 Activator.CreateInstance 等。
我遇到的问题having 是我如何重用现有实例,例如,Word 已经打开,我想使用该实例。
是否有 Activator.UseExistingInstance?或者类似的东西?
谢谢!
i am developing some code in c# where i will be interacting with Microsoft Word. I want to be able to have the option of re-using an existing instance or as an alternative creating a new instance.
Keeping in mind i want to do all of this using LATE BINDING... it is safe to say i have figured out how to get things working when creating a new instance.. i just call Activator.CreateInstance etc..
The problem i am having is how do i reuse an existing instance, for example, Word is already open and i want to use that instance.
Is there an Activator.UseExistingInstance? or something similar??
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在寻找 Marshal.GetActiveObject。
You're looking for Marshal.GetActiveObject.
您可能想查看 Oleacc.dll 中定义的
AccessibleObjectFromWindow
API 函数。 安德鲁·怀特查佩尔 有一些关于如何使用它的文章。根据他的文章,我写了一个非常相似的问题的答案(关于 Excel,而不是 Word),您可以在这里找到:在这里您将找到一个示例,说明如何连接到已运行的 Excel 实例,然后使用后期绑定自动化该实例。
更新:
这是一个适用于 Word 的简短示例:
You might want to have a look at the
AccessibleObjectFromWindow
api function defined in Oleacc.dll. Andrew Whitechapel has some articles on how to use it. Based on his articles I wrote an answer to a very similar question (about Excel, not Word), which you can find here:There you will find an example how to connect to an already running Excel instance and then automating this instance using late binding.
Update:
Here is a short sample adapted to Word: