Ruby Office Automation - 无法让事件处理程序在 MSWord 中工作

发布于 2024-09-18 04:53:45 字数 725 浏览 3 评论 0原文

Ruby 版本 1.9.1p430 在 Office 2010 的 W7 上运行。

我试图捕获 DocumentBeforeClose 事件,但我什至无法接近让它工作!

我遵循了一些使用 Excel 处理 SheetSelectionChange 的示例。它工作没有任何问题。

我想使用相同的过程来处理上述 Word 事件。

我的代码在 WIN32OLE_EVENT.new 语句处失败。这是我正在使用的:

require 'win32ole'

wd = WIN32OLE.connect('Word.Application')
wd.visible = true
doc = wd.Documents.Add

ev = WIN32OLE_EVENT.new(doc, 'ApplicationEvents4')

我得到的错误是:

ev = WIN32OLE_EVENT.new(doc, 'ApplicationEvents4')
RuntimeError: failed to query IConnectionPoint
    HRESULT error code:0x80040200

我在 WIN32OLD_EVENT 语句中广泛查找了接收器名称的正确内容,但这是我想出的最好的内容,它是显然不正确!

如果有人能指出我正确的方向,我将不胜感激。我很想知道是否有人成功处理 Ruby 中的 Word 事件。

Ruby version 1.9.1p430 running on W7 with Office 2010.

I am trying to catch the DocumentBeforeClose event but I cannot even get close to get it to work!

I have followed some examples using Excel where the SheetSelectionChange is handled. It works without any problem.

Using the same process, I wanted to handle the above Word event.

My code fails at the WIN32OLE_EVENT.new statement. Here is what I am using:

require 'win32ole'

wd = WIN32OLE.connect('Word.Application')
wd.visible = true
doc = wd.Documents.Add

ev = WIN32OLE_EVENT.new(doc, 'ApplicationEvents4')

The error I get is:

ev = WIN32OLE_EVENT.new(doc, 'ApplicationEvents4')
RuntimeError: failed to query IConnectionPoint
    HRESULT error code:0x80040200

I have looked extensively for the correct content for the name of the sink in the WIN32OLD_EVENT statement but this is the best I have come up with and it's obviously incorrect!

I would be most grateful if someone can point me in the right direction. I would be interested to hear if anyone has managed to succesfully handle Word events from Ruby.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巡山小妖精 2024-09-25 04:53:45

我认为您应该在 WIN32OLE_EVENT 调用中使用 Word 对象而不是 doc 对象,因为“ApplicationEvents4”与 Word.Application 相关。

ev = WIN32OLE_EVENT.new(wd, 'ApplicationEvents4')

附注
不要忘记消息循环

loop do
    WIN32OLE_EVENT.message_loop
end

I think you should use Word object instead of doc object in WIN32OLE_EVENT call, because of 'ApplicationEvents4' relates to Word.Application.

ev = WIN32OLE_EVENT.new(wd, 'ApplicationEvents4')

PS
Don't forget about message loop

loop do
    WIN32OLE_EVENT.message_loop
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文