Ticket #3043: Excel_test.au3

File Excel_test.au3, 1.7 KB (added by apoliemans, 5 years ago)

FOR..IN..NEXT loop deleting Excel sheets

Line 
1#include <Debug.au3>
2
3_DebugSetup("Debug Out", False, 1)
4_DebugOut("Debug start")
5
6Global $oMyError = ObjEvent("AutoIt.Error","MyErrFunc")
7
8;-----------------------------------------------------------------------
9; Setup Excel environment
10;-----------------------------------------------------------------------
11$oExcel = ObjCreate("Excel.Application")
12$oExcel.Visible = True
13$oExcel.SheetsInNewWorkbook = 1
14
15;-----------------------------------------------------------------------
16; Create workbook and fill with sheets
17;-----------------------------------------------------------------------
18$oWB = $oExcel.workbooks.add
19for $i = 1 to 5
20   $oWB.Sheets.Add
21   _DebugOut("Now " & $oWB.Worksheets.Count & " sheets in workbook")
22   Sleep(1000)
23Next
24
25;-----------------------------------------------------------------------
26; Delete all sheets using FOR .. IN .. NEXT loop
27; Will crash deleting last sheets but that is not the issue here and I decided to leave it in
28;-----------------------------------------------------------------------
29For $sheet In $oWB.Worksheets
30   $sheet.Delete
31   _DebugOut("Now " & $oWB.Worksheets.Count & " sheets in workbook")
32   Sleep(1000)
33Next
34
35Exit
36
37;-----------------------------------------------------------------------
38; Error Handler
39;-----------------------------------------------------------------------
40Func MyErrFunc($oMyError)
41Local $HexNumber
42Local $strMsg
43
44$HexNumber = Hex($oMyError.Number, 8)
45$strMsg = "Error Number: " & $HexNumber & @CRLF
46$strMsg &= "WinDescription: " & $oMyError.WinDescription & @CRLF
47$strMsg &= "Script Line: " & $oMyError.ScriptLine & @CRLF
48MsgBox(0, "ERROR", $strMsg)
49SetError(1)
50
51Endfunc