Can event listener be registered with ActiveX control on (e.g.) an Excel worksheet

11 views (last 30 days)
I can register Excel server, workbook, and worksheet events and the callbacks get called. However, if I have an ActiveX control on a worksheet, I cannot register an event for it - even though I can see which events are available - and have the callback called when they fire (if they are firing). Below shows a series of steps to register and react to a worksheet event and then my attempt to register an event with the OLE control. Any ideas? (Note I have closed up the MATLAB output as much as possible to save space.)
>> x=actxserver('Excel.Application')
>> x.Visible=1
>> hb=x.Workbooks.Open('c:\myfolder\mybook.xlsm')
>> hs=hb.ActiveSheet
>> hs.events
... some events
Deactivate = void Deactivate()
... and several more
>> hs.registerevent({'Deactivate', @eventcallback})
>> hs.eventlisteners
ans = 'Deactivate' @eventcallback
'Excel event occurred' % Message from event handler, eventcallback.m
>> hs.OLEObjects.Item(1).Object.Caption
ans = My pushbutton % Caption on my ActiveX pushbutton control
>> hs.OLEObjects.Item(1).Object.events
... some events
Click = void Click()
... and several more
>> hs.OLEObjects.Item(1).Object.registerevent({'Click', @eventcallback})
>> hs.OLEObjects.Item(1).Object.eventlisteners
ans =
{} % Does not register listener callback much less call it (also no complaints from MATLAB, Excel, etc)
  2 Comments
Guillaume
Guillaume on 8 Sep 2015
People with enough reputation can edit all posts. If you look at your profile, you'll see the reputation required to earn each type of privilege.
The only thing I did is edit the formatting of your post to make it more readable (removed blank lines between lines of code).

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 8 Sep 2015
It looks like you need to acquire the interface of the activex control explicitly in order to register events. In other words, this does not work:
hs.OLEObjects.Item(1).Object.registerevent({'Click', @eventcallback})
but this appears to:
o = hs.OLEObjects.Item(1).Object;
registerevent(o, {'Click', @eventcallback}); %or o.registerevent(...)
No idea why, matlab sometimes exhibits some very odd behaviour with regards to COM.
  1 Comment
Dave Watson
Dave Watson on 8 Sep 2015
Edited: Dave Watson on 8 Sep 2015
Isn't THAT interesting. Of course, it's exactly what I did - without noticing the difference - to register the workbook, worksheet, etc events, which worked. Also, o.eventlisteners lists them but hs.OLEObjects.Item(1).Object.eventlisteners returns {}. Strange. Anyway, thanks a lot - that's great!!

Sign in to comment.

More Answers (0)

Categories

Find more on Event Functions in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!