views:

38

answers:

2

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 its 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.

Thanks and regards

Graham Jones

+1  A: 

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
Sergey Mirvoda
@grimbo: There should be a tick on the left hand side of this question. You can click the tick if the bug is bagged.
Andrew Grimm
A: 

Many thanks. That was the problem.

Regards

Graham Jones

@grimbo you should mark my answer as right.
Sergey Mirvoda