开发者

Ruby Office Automation - cannot get event handlers to work in MSWord

开发者 https://www.devze.com 2023-01-15 16:19 出处:网络
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!

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.n开发者_StackOverflowew(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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号