Azzera filtri
Azzera filtri

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

10 visualizzazioni (ultimi 30 giorni)
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 Commenti
Guillaume
Guillaume il 8 Set 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).

Accedi per commentare.

Risposta accettata

Guillaume
Guillaume il 8 Set 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 Commento
Dave Watson
Dave Watson il 8 Set 2015
Modificato: Dave Watson il 8 Set 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!!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Event Functions in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by