maniootek

_Excel_Open() cause COM error if no Excel instance

3 posts in this topic

#1 ·  Posted

This piece of code generate com error if there is no excel instance

#Include <Excel.au3>

Local $oErr = ObjEvent("AutoIt.Error", "_ErrFunc")
$oExcel = _Excel_Open()

Func _ErrFunc($oError)
    local $message = "COM Error!" & @CRLF 
    $message &= "number: " & $oError.number & @CRLF
    $message &= "windescription: " & $oError.windescription & @CRLF
    $message &= "scriptline: " & $oError.scriptline & @CRLF
    MsgBox(16, @ScriptName, $message)
EndFunc   ;==>_ErrFunc

any Idea what could be wrong?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

It seems to be happening in regards to the last parameter ($bForceNew) which if not set to true attempts to connect to an existing open instance of Excel.  If you don't want to have to set the parameter I would suggest you put in a condition in the _ErrFunc to omit this error.  Should be safe as the next line in the Excel UDF will start a new instance if the previous line had an error.

Examples:

;Omit Excel Attach Error
#Include <Excel.au3>
Const $EXCEL_ATTACH_FAILED = -2147352570

Local $oErr = ObjEvent("AutoIt.Error", "_ErrFunc")
$oExcel = _Excel_Open()

Func _ErrFunc($oError)
    If $oError.number = $EXCEL_ATTACH_FAILED Then Return 0
    local $message = "COM Error!" & @CRLF
    $message &= "number: " & $oError.number & @CRLF
    $message &= "windescription: " & $oError.windescription & @CRLF
    $message &= "scriptline: " & $oError.scriptline & @CRLF
    MsgBox(16, @ScriptName, $message)
EndFunc   ;==>_ErrFunc
;Force new instance
#Include <Excel.au3>

Local $oErr = ObjEvent("AutoIt.Error", "_ErrFunc")
$oExcel = _Excel_Open(Default,Default,Default,Default,True)

Func _ErrFunc($oError)
    local $message = "COM Error!" & @CRLF
    $message &= "number: " & $oError.number & @CRLF
    $message &= "windescription: " & $oError.windescription & @CRLF
    $message &= "scriptline: " & $oError.scriptline & @CRLF
    MsgBox(16, @ScriptName, $message)
EndFunc   ;==>_ErrFunc

 

Edited by spudw2k

Share this post


Link to post
Share on other sites

#3 ·  Posted

Allright, thank you.

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