Connects a COM object's event sources to functions with a given prefix.

ComObjConnect ComObject , Prefix



Type: Object

An object which raises events.

If the object does not support the IConnectionPointContainer interface or type information about the object's class cannot be retrieved, an error message is shown. This can be suppressed or handled with try/catch.

The IProvideClassInfo interface is used to retrieve type information about the object's class if the object supports it. Otherwise, ComObjConnect attempts to retrieve type information via the object's IDispatch interface, which may be unreliable.


Type: String or Object

A string to prefix to the event name to determine which function to call when an event occurs.

If omitted, the object is "disconnected"; that is, the script will no longer receive notification of its events.

This parameter can be an object defined by the script. When an event is raised, the corresponding method is called. The first parameter, which is usually the hidden this parameter, refers to the script-defined object, not the COM object. To catch all events without defining a method for each one, define a __Call meta-function.


To make effective use of ComObjConnect, you must first write functions in the script to handle any events of interest. Such functions, or "event-handlers," have the following structure:

PrefixEventName([Params..., ComObject])
    ... event-handling code ...
    return ReturnValue

Prefix is a prefix of your choosing, while EventName is the name of whatever event the function should handle.

Params corresponds to whatever parameters the event has. If the event has no parameters, Params should be omitted entirely. ComObject is an additional parameter containing a reference to the original wrapper object which was passed to ComObjConnect; it is never included in the COM event's documentation. "ComObject" should be replaced with a name more meaningful in the context of your script.

Note that event handlers may have return values. To return a COM-specific type of value, use ComObject(type, value). For example, return ComObject(0,0) returns a variant of type VT_EMPTY, which is equivalent to returning undefined (or not returning) from a JavaScript function.

Call ComObjConnect(yourObject, "Prefix") to enable event-handling.

Call ComObjConnect(yourObject) to disconnect the object (stop handling events).

If the number of parameters is not known, a variadic function can be used.


The script must retain a reference to ComObject, otherwise it would be freed automatically and would disconnect from its COM object, preventing any further events from being detected. There is no standard way to detect when the connection is no longer required, so the script must disconnect manually by calling ComObjConnect.

The #Persistent directive may be needed to keep the script running while it is listening for events.

An exception is thrown on failure.


ComObjCreate, ComObjGet, ComObjActive, WScript.ConnectObject (MSDN)



ie := ComObjCreate("InternetExplorer.Application")

; Connects events to corresponding script functions with the prefix "IE_".
ComObjConnect(ie, "IE_")

ie.Visible := true  ; This is known to work incorrectly on IE7.

IE_DocumentComplete(ieEventParam, url, ieFinalParam) {
    global ie
    s := ""
    if (ie != ieEventParam)
        s .= "First parameter is a new wrapper object.`n"
    if (ie == ieFinalParam)
        s .= "Final parameter is the original wrapper object.`n"
    if (ComObjValue(ieEventParam) == ComObjValue(ieFinalParam))
        s .= "Both wrapper objects refer to the same IDispatch instance.`n"
    MsgBox s . "Finished loading " ie.Document.title " @ " url