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

조회 수: 10 (최근 30일)
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
Guillaume
Guillaume 2015년 9월 8일
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 2015년 9월 8일
I think I edited it to reformat it before Guillaume did.

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2015년 9월 8일
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
Dave Watson
Dave Watson 2015년 9월 8일
편집: Dave Watson 2015년 9월 8일
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!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Event Functions에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by