ip._setevents(["eventfolder"] [flags])
ip - interface pointer variable.
eventfolder - where are event functions. Can be:
1 | don't allow multiple connections on the same object. |
Usually you call COM object's functions ("methods" and "properties"). Some COM objects also support "events", that is, can call your functions (event functions). But at first need to connect object's events to your functions.
_setevents connects or disconnects object's events.
Example:
Excel.Application x._create x._setevents("sub.x")
When object (in the example - variable x) fires an event, the corresponding function from eventfolder (in the example - a sub-function) is called.
Creating a new event function:
Usually COM objects work only in single thread (running macro). To receive events, thread where the object is created must be running. If it ends immediately, events have no sense. For example, you can use a dialog. Also you can use wait, but then need to insert opt waitmsg 1 before, or events will be blocked. See example.
To access the event-source object variable from an event function, use one of:
function x y Excel.Application'x x.Function()
By default, multiple calls to _setevents create multiple connections. For example, you can connect two folders simultaneously. If flag 1 is set, previous connection is deleted before making new.
To disconnect, call _setevents without arguments. Usually it is not necessary because it happens automatically when the object is deleted.
Events can be used only with coclasses (icon ) from type libraries.
_setevents must be used directly with a variable. Code like var.Function._setevents(...) will not work.
ExcelSheet es.Init Excel.Worksheet x=es.ws x._setevents("sub.x") opt waitmsg 1 10 #sub x_SelectionChange function Excel.Range'Target ;;Excel._Worksheet'x out 1
\Dialog_Editor str controls = "3" str ax3SHD ax3SHD="http://www.quickmacros.com" if(!ShowDialog("" &sub.DialogProcedure &controls)) ret BEGIN DIALOG 0 "" 0x90C80AC8 0x0 0 0 224 136 "Dialog" 3 ActiveX 0x54030000 0x0 0 0 224 114 "SHDocVw.WebBrowser" 1 Button 0x54030001 0x4 116 116 48 14 "OK" 2 Button 0x54030000 0x4 168 116 48 14 "Cancel" END DIALOG DIALOG EDITOR: "" 0x2040104 "*" "" "" "" #sub DialogProcedure function# hDlg message wParam lParam sel message case WM_INITDIALOG SHDocVw.WebBrowser we3 we3._getcontrol(id(3 hDlg)) we3._setevents("sub.we3") case WM_DESTROY case WM_COMMAND goto messages2 ret messages2 sel wParam case IDOK case IDCANCEL ret 1 #sub we3_DocumentComplete function IDispatch'pDisp `&URL ;;SHDocVw.IWebBrowser2'we3 out URL