Saturday, April 10, 2010

VBScript - Verify That an Excel Worksheet Exists

VBScript - Verify That an Excel Worksheet Exists (Verifies that an Excel worksheet exists.)

'* Script name: WorksheetExists.vbs
'* Created on: 10/24/2006
'* Author: Kent Finkle
'* Purpose: Verify that an Excel Worksheet exists.

If SheetExists("C:\scripts\test.xls","Sheet1") Then
Wscript.Echo "Exists"
Else
Wscript.Echo "Does not exist"
End If

If SheetExists("C:\scripts\test.xls","Sheet2") Then
Wscript.Echo "Exists"
Else
Wscript.Echo "Does not exist"
End If

Function SheetExists(strFileName,strSheetName)

Dim objExcel
Dim objWorkbook
Dim objWorksheet
Dim blnSheetExists

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\scripts\test.xls")

SheetExists = False

For Each objWorksheet In objWorkbook.Worksheets

If objWorksheet.Name = strSheetName Then
SheetExists = True
Exit For
End If

Next

objExcel.Quit

End Function

[Via]