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

1

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.

ruby
win32ole
office-automation
asked on Stack Overflow Sep 7, 2010 by Graham • edited Nov 23, 2011 by Andrew Grimm

1 Answer

1

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
answered on Stack Overflow Sep 7, 2010 by Sergey Mirvoda

User contributions licensed under CC BY-SA 3.0