| 1 | | As the Excel UDF is being brushed up this seems to be the right moment to reopen and solve this ticket. |
| | 1 | The _ExcelSheetAddNew() function abnormally terminates script execution with a COM error if called specifying a sheet name that already exists within the workbook. Also, after the error, the workbook will erroneously contain a new generically named sheet ("Sheet 1", "Sheet 2", etc). |
| | 2 | |
| | 3 | The following change adds a loop to check if the sheet is already in the workbook, and kicks out a new @error value (@error = 2) if found. |
| | 4 | |
| | 5 | {{{ |
| | 6 | ; #FUNCTION# ==================================================================================================================== |
| | 7 | ; Name...........: _ExcelSheetAddNew |
| | 8 | ; Description ...: Add new sheet to workbook - optionally with a name. |
| | 9 | ; Syntax.........: _ExcelSheetAddNew($oExcel[, $sName = ""]) |
| | 10 | ; Parameters ....: $oExcel - An Excel object opened by a preceding call to _ExcelBookOpen() or _ExcelBookNew() |
| | 11 | ; $sName - The name of the sheet to create (default follows standard Excel new sheet convention) |
| | 12 | ; Return values .: Success - Returns 1 |
| | 13 | ; Failure - Returns 0 and sets @error on errors: |
| | 14 | ; |@error=1 - Specified object does not exist |
| | 15 | ; |@error=2 - Specified sheet already exists |
| | 16 | ; Author ........: SEO <locodarwin at yahoo dot com> |
| | 17 | ; Modified.......: litlmike, Spiff59 |
| | 18 | ; Remarks .......: None |
| | 19 | ; Related .......: |
| | 20 | ; Link ..........: |
| | 21 | ; Example .......: Yes |
| | 22 | ; =============================================================================================================================== |
| | 23 | Func _ExcelSheetAddNew($oExcel, $sName = "") |
| | 24 | If Not IsObj($oExcel) Then Return SetError(1) |
| | 25 | If $sName Then |
| | 26 | Local $iTemp = $oExcel.ActiveWorkbook.Sheets.Count |
| | 27 | For $xx = 1 To $iTemp |
| | 28 | If $oExcel.ActiveWorkbook.Sheets($xx).Name = $sName Then |
| | 29 | Return SetError(2) |
| | 30 | EndIf |
| | 31 | Next |
| | 32 | EndIf |
| | 33 | Local $oSheet = $oExcel.ActiveWorkBook.WorkSheets.Add().Activate() |
| | 34 | If $sName Then $oExcel.ActiveSheet.Name = $sName |
| | 35 | Return 1 |
| | 36 | EndFunc ;==>_ExcelSheetAddNew |
| | 37 | }}} |