Skip to main content

Get the Excel Worksheet name

This example script shows how to obtain an excel worksheet name.  This can be handy if importing an excel worksheet and you don't know the worksheets name.

Snippet
Sub Main
	Dim obj As Object
	Dim task As task
	Dim oBook As Object
	Dim oSheet As Object
	Dim sFilename As String
	
	Set obj = Client.CommonDialogs
	sFilename = obj.FileOpen("","","Excel Files (*.xls; *.xlsx)|*.xls; *.xlsx||;")
	Set obj = Nothing 
	
	If sFilename <> "" Then
	
		Set excel = CreateObject("Excel.Application") 'open the excel object to read the worksheet name
		' Get the next file name and place it in FileList.
		Set oBook = excel.Workbooks.Open(sFilename)
		Set oSheet = oBook.Worksheets.Item(1)
		sWorkSheet = oSheet.Name
		oBook.Close (True)
		excel.quit
		Set oBook = Nothing
		Set oSheet = Nothing
		MsgBox "The first worksheet: " & sWorkSheet & " for file: " & sFilename
	Else
		MsgBox "No excel file selected"
	End If
End Sub