soonyee91 Posted June 1, 2013 Author Share Posted June 1, 2013 I think there is some problem in the func code. I can't seem to get what I want. Is there a way to ignore the error and proceed with the script. I do remember that Vba has this "On error resume next". Is there a way to ignore this error in autoit even when $rng = 0? Link to comment Share on other sites More sharing options...
water Posted June 1, 2013 Share Posted June 1, 2013 You need to add a COM error handler to your script. See ObjEvent in the help file for an example. My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
soonyee91 Posted June 2, 2013 Author Share Posted June 2, 2013 (edited) So far I have able to trap the com error using the method you suggest to me. However, it will clear the whole page of my sheet that does not contain printarea. Can you point it out where is my mistake? MyErrFunc will trap the error and continue to execute the code for that specific line which I don't want. Is it possible to resume to next loop after error encounter(what I mean is just skip those error line?? Is there any ways to suppress that error and proceed? Func MyErrFunc($oMyError) Local $HexNumber Local $strMsg $HexNumber = Hex($oMyError.Number, 8) $strMsg = "Error Number: " & $HexNumber & @CRLF $strMsg &= "WinDescription: " & $oMyError.WinDescription & @CRLF $strMsg &= "Script Line: " & $oMyError.ScriptLine & @CRLF MsgBox(0, "ERROR", $strMsg) SetError(1) EndFunc Func _ClearOutsidePrintArea() Edited June 2, 2013 by soonyee91 Link to comment Share on other sites More sharing options...
water Posted June 2, 2013 Share Posted June 2, 2013 If you trap the error AutoIt simply continues with the next statement. So you still have to check for success after you retrieved the PrintArea: Local $rng =$oExcel.ActiveSheet.PageSetup.PrintArea If @error or Not IsObj($rng) Then Return SetError(1,0,'') My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
soonyee91 Posted June 2, 2013 Author Share Posted June 2, 2013 (edited) Like you said:"If you trap the error AutoIt simply continues with the next statement." How should I trap the error and simply skip it and continue with the next statement? My code structure is open the selected opened workbok. Then perform this clearPrintarea function . But the problem is the code will not sustain empty printarea and do not suppress the code or skip. Can you provide a better solution? Or should I do in this way?? create 2 function, One is with the condition that has print area, another is with the condition that do not has the print area Edited June 2, 2013 by soonyee91 Link to comment Share on other sites More sharing options...
water Posted June 2, 2013 Share Posted June 2, 2013 Some detailed information is needed. Which version of Excel do you run? Which version of AutoIt do you run? My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
soonyee91 Posted June 2, 2013 Author Share Posted June 2, 2013 My intention of this script is to run excel version 2000 but I do try it in 2010(it works though) My version of autoit is 3.3.8.1 Link to comment Share on other sites More sharing options...
water Posted June 2, 2013 Share Posted June 2, 2013 (edited) I'm running Excel 2010. If no PrintArea is set by the user then the whole sheet is returned. So no error handler seems to be needed. This works fine for me with/without a pre-set PrintArea by the user. #include <Excel.au3> Global $oExcel = _ExcelBookOpen(@ScriptDir & "\_Excel4.xls") ;$oExcel.ActiveSheet.PageSetup.PrintArea = "A1:B2" ; manually set a PrintArea _ClearOutsidePrintArea() Exit Func _ClearOutsidePrintArea() Local $rng = $oExcel.Range($oExcel.ActiveSheet.PageSetup.PrintArea) If @error Or Not IsObj($rng) Then Return SetError(1, 0, '') Local $FirstEmptyCol = $rng.Columns.Count + 1 Local $FirstEmptyRow = $rng.Rows.Count + $rng.Cells(1).Row $oExcel.Range($oExcel.Cells(1, $FirstEmptyCol), $oExcel.Cells(1, 256)).Entirecolumn.clear $oExcel.Range($oExcel.Cells($FirstEmptyRow, 1), $oExcel.Cells($oExcel.Rows.Count, 1)).EntireRow.clear EndFunc ;==>_ClearOutsidePrintArea Edited June 2, 2013 by water My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
Solution water Posted June 2, 2013 Solution Share Posted June 2, 2013 A small correction is needed. If the worksheet is empty, then a COM error will be raised. So now the script has to look like: #include <Excel.au3> Global $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc") Global $oExcel = _ExcelBookOpen(@ScriptDir & "\_Excel4.xls") ;$oExcel.ActiveSheet.PageSetup.PrintArea = "A1:B2" ; manually set a PrintArea _ClearOutsidePrintArea() Exit Func _ClearOutsidePrintArea() Local $rng = $oExcel.Range($oExcel.ActiveSheet.PageSetup.PrintArea) If @error Or Not IsObj($rng) Then Return SetError(1, 0, '') Local $FirstEmptyCol = $rng.Columns.Count + 1 Local $FirstEmptyRow = $rng.Rows.Count + $rng.Cells(1).Row $oExcel.Range($oExcel.Cells(1, $FirstEmptyCol), $oExcel.Cells(1, 256)).Entirecolumn.clear $oExcel.Range($oExcel.Cells($FirstEmptyRow, 1), $oExcel.Cells($oExcel.Rows.Count, 1)).EntireRow.clear EndFunc ;==>_ClearOutsidePrintArea Func _ErrFunc() EndFunc ;==>_ErrFunc My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
soonyee91 Posted June 2, 2013 Author Share Posted June 2, 2013 Thanks man!!!! I when use your script and run it. No problem occurs. However when I run on my original script problem comes out. My final solution is moved my part of code to suite yours. I think most likely there is something wrong in my part. After this moving section. Everything is solve. Thanks for guiding me all the way!!! Link to comment Share on other sites More sharing options...
water Posted June 2, 2013 Share Posted June 2, 2013 It has taken a few rounds but I'm glad that your problem could be solved My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki 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