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

10 Ansichten (letzte 30 Tage)
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 Kommentare
Guillaume
Guillaume am 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).
Walter Roberson
Walter Roberson am 8 Sep. 2015
I think I edited it to reformat it before Guillaume did.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Guillaume
Guillaume am 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 Kommentar
Dave Watson
Dave Watson am 8 Sep. 2015
Bearbeitet: Dave Watson am 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!!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Event Functions finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by