Jump to content
Sign in to follow this  

Failing to hook Excel events.

Recommended Posts



Could anyone shed some light on this problem which has had me spinning my wheels for

about the last week? I'm trying, and failing, to hook Excel events. I've managed it

with Word, and have combined both in the scenario script below. Word sails through and

I get the console output from my MSWEvent_() function. OTOH, the equivalent for Excel

produces a COM error 80040200 (no meaningful message) after what seems a timeout on

the ObjEvent() call. By way of testing system configuration, I have done the equivalent

in VBA within Word, where I can trap Excel events without problems. The problem is

apparent at home (Win 98/Office 97) and work (Win XP/Office 2k).

#include <GUIConstants.au3>

$oMyError = ObjEvent("AutoIt.Error","MyErrFunc")   ; Initialize a COM error handler

$oWD = ObjCreate("Word.Application")
$WDfilein = "C:\My WORD.doc"; must exist
With $oWD
    .Visible = True
    .Caption = "My New Word title"
$oWordEvts = ObjEvent($oWD,"MSWEvent_")
; No Errors here!

$oXL = ObjCreate("Excel.Application")
$XLfilein = "C:\myXLS.xls"; must exist
With $oXL
    .Visible = True
    .Caption = "My New XL title"
$oExcelEvts = ObjEvent($oXL,"XLEvent_")
;~ We intercepted a COM Error !
;~ err.number is:   80040200
;~ err.lastdllerror is:     0
;~ err.scriptline is:   21

$Form1 = GUICreate("IdleForm", 200, 200, 194, 116)
While 1
    $msg = GuiGetMsg()
    Case $msg = $GUI_EVENT_CLOSE


func MSWEvent_($Eventname)
    ConsoleWrite("Unhandled MSWord Event: " & $Eventname & @LF)

func XLEvent_($Eventname)
    ConsoleWrite("Unhandled MSExcel Event: " & $Eventname & @LF)

Func MyErrFunc()
Dim $msgTxt
    $msgTxt =   "We intercepted a COM Error !"    & @CRLF  & @CRLF & _
             "err.description is: " & @TAB & $oMyError.description  & @CRLF & _
             "err.windescription:"   & @TAB & $oMyError.windescription & @CRLF & _
             "err.number is: "       & @TAB & hex($oMyError.number,8)  & @CRLF & _
             "err.lastdllerror is: "   & @TAB & $oMyError.lastdllerror   & @CRLF & _
             "err.scriptline is: "   & @TAB & $oMyError.scriptline   & @CRLF & _
             "err.source is: "       & @TAB & $oMyError.source       & @CRLF & _
             "err.helpfile is: "       & @TAB & $oMyError.helpfile     & @CRLF & _
             "err.helpcontext is: " & @TAB & $oMyError.helpcontext
    Msgbox(0,"AutoItCOM Test", $msgTxt)
    ConsoleWrite($msgTxt & @LF)
    Local $err = $oMyError.number
    If $err = 0 Then $err = -1
    $g_eventerror = $err ; to check for after this function returns

Share this post

Link to post
Share on other sites


Still all quiet after 2 weeks. Time to own up, before I get a reply calling me an Ejit, that I've realised I posted to the wrong forum. The significance of the second 'X' in the forum title didn't hit me until after I hit 'Send'. Sorry.

Perhaps in the correct forum, someone with a copy of Excel might have read it and wondered...?



Share this post

Link to post
Share on other sites


I tried your script on WinXP with Office 2007 and received same results:

Unhandled MSWord Event: WindowDeactivate

We intercepted a COM Error !

err.description is:

err.windescription: ÌÍ??????$??

err.number is: 80040200

err.lastdllerror is: 0

err.scriptline is: 22

err.source is:

err.helpfile is:

err.helpcontext is:

I hope this is helpful

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.