Sign in to follow this  
Followers 0
Cavok

Error opening Excel workbook

5 posts in this topic

I am using the following piece of code to open an Excel file

$oExcel = ObjCreate("Excel.Application")
$oExcel.Workbooks.Open($filename).Sheets(1).Select()                     <<------- CRASH (see below)
$oExcel.Visible = True
If @error Then
   MsgBox(4096, "Error", "Error")
   Exit
EndIf

The Excel file is password protected and while opening the file the user is asked if the file has to be openend in read-only mode or to cancel opening the file. While choosing for opening in read-only mode this gives no problems. However, when canceling opening the file my AutoIT script (and thus the application from which the Excel file has been opened) crashes at the line as indicated above with the following error message:

The requested action with this object has failed.

It appears that there is no way to catch the error with the @error flag, so are there other ways that I can use to prevent my script from crashing when this situation happens? Thanks in advance!

Share this post


Link to post
Share on other sites



Nobody? :)

Share this post


Link to post
Share on other sites

Welcome to the forums!

You need to register your own COM error handler.

Read the OBJ/COM Reference in the help file, as well as the example for ObjEvent.


[font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font]

Share this post


Link to post
Share on other sites

Your script with an error handler (adapted straight from the one in the help file):

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

$oExcel = ObjCreate("Excel.Application")
$oExcel.Workbooks.Open ($filename).Sheets (1).Select ()
$oExcel.Visible = True
If @error Then
    MsgBox(4096, "Error", "Error")
    Exit
EndIf

; This is my custom error handler
Func MyErrFunc()
    $HexNumber = Hex($oMyError.number, 8)
    MsgBox(16, "COM Error", "AutoIt intercepted a COM Error !" & @CRLF & _
            "Number is: " & $HexNumber & @CRLF & _
            "Windescription is: " & $oMyError.windescription & @CRLF & _
            "Source Obj is: " & $oMyError.windescription & @CRLF & _
            "Description is: " & $oMyError.windescription)

    Return SetError(1) ; something to check for when this function returns
EndFunc   ;==>MyErrFuncoÝ÷ ØhmºÇÅÇ¥ç!nëb²ØZ¶·z·§qëh¶°¢¹¢/jXANDL¨º^+H槲.Ù÷öØ­mê)zwmçè­ëç-x,ç­³g²Û|¸¤y«­¢+ØÀÌØí½á°ô=©
ÉÑ ÅÕ½Ðíá°¹ÁÁ±¥Ñ¥½¸ÅÕ½Ðì¤(ÀÌØí½]½É­½½¬ôÀÌØí½á°¹]½É­½½­Ì¹=Á¸ ÀÌØí¥±¹µ¤(ÀÌØí½]½É­½½¬¹M¡ÑÌ Ä¤¹M±Ð ¤

...or some such?

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Thanks!! The COM error handler solved my problem! I am new to AutoIt and didn't know about the creation of error handlers yet.

I don't use Excel much, but is that reference to the workbook valid BEFORE you open it? I mean, shouldn't it be opened before selecting sheets 'n stuff?

As far as I know your example code is in fact similar to mine, as the statement is executed from left to right. So first the Open function is executed which creates the Excel object and then the Sheets(1).Select() function is executed on the just created Excel object. At least this is how it works in Object Oriented languages.....

Anyway, it works so both thanks for the input! :)

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  
Followers 0