Workbook Access – Get First Sheet Name
Retrieves the name of the first worksheet in the specified Excel workbook.
This function opens the workbook using a new instance of Excel.Application (with Visible = False ), reads the name of the first sheet (index 1), and then closes the workbook. Errors during loading or access are silently caught and result in a Nothing return value.
VB
Public Function xlGetFirstSheetName(WorkbookPath As String) As String
Try
Dim App As New Excel.Application
App.Visible = False
Dim wbBook = App.Workbooks.Open(WorkbookPath)
Dim wsName As String = wbBook.Sheets(1).Name
wbBook.Close()
App = Nothing
Return wsName
Catch ex As Exception
Dim szFunctionName As String = New Diagnostics.StackTrace().GetFrame(0).GetMethod().Name
'ShowMessageBox(szModuleName + "." + szFunctionName + vbLf + vbLf + ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Return Nothing
End Function