フィルターのクリア

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

10 ビュー (過去 30 日間)
Dave Watson
Dave Watson 2015 年 9 月 6 日
編集済み: Dave Watson 2015 年 9 月 8 日
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 Center および File ExchangeEvent Functions についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by