ObjectEventRemove

Cancels the association between a user-defined function or subroutine and a COM object event.

Syntax:

ObjectEventRemove(object-reference, event-name)

Parameters:

(i) object-reference variable containing an object reference

(s) event-name name of event being handled as a string

Returns:

(i) result 1 on success, 0 on failure

 

ObjectEventRemove breaks the previously established link between a user-defined function or subroutine and a COM object event. Once the link is broken, your user-defined function or subroutine will no longer execute when the COM object fires the indicated event.

You can also break the link between an event and your event handling function or subroutine by releasing all references to the COM object associated with the event. This is usually done by assigning a zero (0) to all variables holding a reference to the COM object. This function is provided for the occasion where you wish to stop handling an event but wish to continue using the COM object in your script.

object-reference

The first parameter is a reference to the object whose event you wish to stop handling. The parameter must be a variable containing an object reference and the reference must have been previously used in a successful call to ObjectEventAdd.

event-name

The second parameter is the name of the event you wish to stop handling. The event name must have been previously used in a successful call to ObjectEventAdd.

Example:


;This example illustrates a script wrapper for Excel which uses the
;ObjectEventRemove function to remove events when the initial Excel workbook
;is closed. The script also demonstrates that is possible to remove events
;from within event handlers.
#DefineSubRoutine WorkbookBeforeClose(objWorkbook, bCancel)
; This is called when you choose to close the workbook in Excel.
; The event handlers are removed, and then the workbook is closed
; without saving changes.
ObjectEventRemove(objXlApp, "SheetChange")
ObjectEventRemove(objXlApp, "WorkbookBeforeClose")
objWorkbook.Saved = @TRUE ; Set the dirty flag to true so there
; is no prompt to save.
bDone = @TRUE ; End event loop
#EndSubRoutine
#DefineSubRoutine SheetChange(objSheet, objTarget)
;This is called when a cell or cells on a worksheet are changed.
sWhere = StrCat(objTarget.Address, " on ", objTarget.Worksheet.Name())
Message("You Changed Cells", sWhere)
#EndSubRoutine
;Start Excel and create a new workbook.
bDone = @FALSE ; Event loop control variable
objXlApp = ObjectCreate("Excel.Application")
objXlBook = objXlApp.Workbooks.Add()
objXlBook.Windows(1).Caption = "Event Handler Example"
objXlBook.Worksheets.Item(1).Activate()
;Add an event handler for the WorkbookBeforeClose Event of the
;Application object.
ObjectEventAdd(objXlApp,"WorkbookBeforeClose","WorkbookBeforeClose")
; Add an event handler for the SheetChange event of all Worksheet
; objects.
ObjectEventAdd(objXlApp,"SheetChange","SheetChange")
;Make Excel visible and give the user control.
objXlApp.Visible = @TRUE
objXlApp.UserControl = @TRUE
; Event processing loop
While !bDone
   TimeDelay(1)
EndWhile
; Add a fresh workbook before exiting in case
; the user closed the workbook but not Excel
objXlBook = objXlApp.Workbooks.Add()
objXlBook.Windows(1).Caption = "Event Handler Example (No Events)"
objWorkbook = 0
objTarget = 0
objSheet = 0
objXlBook = 0
objXlApp = 0
See Also:

ObjectEventAdd, ObjectCreate, ObjectGet