Cavok Posted May 25, 2007 Share Posted May 25, 2007 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! Link to comment Share on other sites More sharing options...
Cavok Posted May 30, 2007 Author Share Posted May 30, 2007 Nobody? Link to comment Share on other sites More sharing options...
Skruge Posted May 30, 2007 Share Posted May 30, 2007 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] Link to comment Share on other sites More sharing options...
PsaltyDS Posted May 30, 2007 Share Posted May 30, 2007 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 Link to comment Share on other sites More sharing options...
Cavok Posted May 31, 2007 Author Share Posted May 31, 2007 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! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now