Results 1 to 4 of 4

Thread: [FAQ's: OD] How do I enumerate the sheets in a workbook without using Excel?

  1. #1

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    [FAQ's: OD] How do I enumerate the sheets in a workbook without using Excel?

    When performing automation on Excel many times we are faced with trying tofind out if a certain sheet exists before executing code against it. To solve this issue we can use ADO or ADO.NET to connect to the workbook and obtain the workbook schema including the table(s). A table is the equilivalent of an Excel sheet/worksheet.







    Using ADO.NET we can get the schema of the workbook and retrieve the sheet names that are in the selected workbook.


    VB.NET 2003 And Excel (97-2003) Code Example:

    VB Code:
    1. Option Explicit On
    2. Option Strict On
    3. 'Copyright © 2006 by RobDog888 (VB/Office Guru™). All Rights reserved.
    4. '
    5. '              You may not reproduce or publish this
    6. '              code on any web site, online service,
    7. '              or distribute as source on any media
    8. '              without express permission.
    9.  
    10. Public Class Form1
    11.  
    12.     Inherits System.Windows.Forms.Form
    13.  
    14.     "Windows Form Designer generated code"
    15.  
    16.     Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
    17.         Me.Close()
    18.     End Sub
    19.  
    20.     Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) _
    21.     Handles MyBase.Closing
    22.         Application.Exit()
    23.     End Sub
    24.  
    25.     Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
    26.         Dim oCnn As New OleDb.OleDbConnection
    27.         Dim schemaTable As DataTable
    28.         Dim dlgOpenFile As New OpenFileDialog
    29.         Dim strFileName As String
    30.         With dlgOpenFile
    31.             .CheckFileExists = True
    32.             .CheckPathExists = True
    33.             .Filter = "Excel Files Only (*.xls)|*.xls"
    34.             .FilterIndex = 0
    35.             .InitialDirectory = Application.StartupPath
    36.             .Multiselect = False
    37.             .Title = "Select an Excel Workbook File"
    38.             If .ShowDialog = DialogResult.OK Then
    39.                 strFileName = .FileName
    40.                 oCnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFileName & _
    41.                 ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""
    42.                 oCnn.Open()
    43.                 schemaTable = oCnn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, _
    44.                 New Object() {Nothing, Nothing, Nothing, "TABLE"})
    45.                 oCnn.Close()
    46.                 ListBox1.Items.Clear()
    47.                 For i As Integer = 0 To schemaTable.Rows.Count - 1
    48.                     ListBox1.Items.Add(schemaTable.Rows(i).Item("TABLE_NAME").ToString)
    49.                 Next
    50.                 Label1.Text = "Sheets in Workbook: " & strFileName.Substring(strFileName.LastIndexOf("\") + 1)
    51.             End If
    52.         End With
    53.     End Sub
    54.  
    55. End Class
    Last edited by RobDog888; Apr 1st, 2007 at 02:31 PM. Reason: Fix coloring tags
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  2. #2

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [FAQ's: OD] How do I enumerate the sheets in a workbook without using Excel?

    VB.NET 2005 and Excel 2007
    VB Code:
    1. Option Explicit On
    2. Option Strict On
    3. 'Copyright © 2006 by RobDog888 (VB/Office Guru™). All Rights reserved.'
    4. '              You may not reproduce or publish this
    5. '              code on any web site, online service,
    6. '              or distribute as source on any media
    7. '              without express permission.
    8. Public Class Form1
    9.  
    10.     Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
    11.         Me.Close()
    12.     End Sub
    13.  
    14.     Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    15.         Application.Exit()
    16.     End Sub
    17.  
    18.     Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
    19.         Dim oCnn As New OleDb.OleDbConnection
    20.         Dim schemaTable As DataTable
    21.         Dim dlgOpenFile As New OpenFileDialog
    22.         Dim strFileName As String
    23.         With dlgOpenFile
    24.             .CheckFileExists = True
    25.             .CheckPathExists = True
    26.             .Filter = "Excel 2007 Files Only (*.xlsx)|*.xlsx"
    27.             .FilterIndex = 0
    28.             .InitialDirectory = Application.StartupPath
    29.             .Multiselect = False
    30.             .Title = "Select an Excel Workbook File"
    31.             If .ShowDialog = DialogResult.OK Then
    32.                 strFileName = .FileName
    33.                 oCnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFileName & ";Extended Properties=""Excel 12.0;HDR=YES"";"
    34.                 oCnn.Open()
    35.                 schemaTable = oCnn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, _
    36.                 New Object() {Nothing, Nothing, Nothing, "TABLE"})
    37.                 oCnn.Close()
    38.                 ListBox1.Items.Clear()
    39.                 For i As Integer = 0 To schemaTable.Rows.Count - 1
    40.                     ListBox1.Items.Add(schemaTable.Rows(i).Item("TABLE_NAME").ToString)
    41.                 Next
    42.                 Label1.Text = "Sheets in Workbook: " & strFileName.Substring(strFileName.LastIndexOf("\") + 1)
    43.             End If
    44.         End With
    45.     End Sub
    46. End Class
    Reference for connectionstring:
    http://msdn2.microsoft.com/en-us/library/aa395290.aspx
    Last edited by RobDog888; Apr 29th, 2007 at 03:58 PM. Reason: Fix code tags from recent upgrade
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [FAQ's: OD] How do I enumerate the sheets in a workbook without using Excel?



    VB 6 And Excel (97-2003) Code Example:
    VB Code:
    1. Option Explicit
    2. 'Copyright © 2006 by RobDog888 (VB/Office Guru™). All Rights reserved.
    3. '
    4. '              You may not reproduce or publish this
    5. '              code on any web site, online service,
    6. '              or distribute as source on any media
    7. '              without express permission.
    8.  
    9. 'Add a CommonDialog control to your form, 2 buttons and 1 label
    10. 'Add a reference to Microsoft ActiveX Data Objects 2.x Library
    11. Private Sub cmdBrowse_Click()
    12.     On Error GoTo MyError
    13.     Dim oCnn As ADODB.Connection
    14.     Dim oRs As ADODB.Recordset
    15.     Dim strFileName As String
    16.    
    17.     With CommonDialog1
    18.         .CancelError = True
    19.         .DialogTitle = "Select an Excel File."
    20.         .Filter = "Excel 97-2003 Format (*.xls)|*.xls"
    21.         .FilterIndex = 1
    22.         .Flags = cdlOFNFileMustExist Or cdlOFNPathMustExist
    23.         .ShowOpen
    24.         strFileName = .FileName
    25.     End With
    26.     Set oCnn = New ADODB.Connection
    27.     oCnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
    28.     strFileName & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""
    29.     oCnn.Open
    30.     Set oRs = oCnn.OpenSchema(adSchemaTables)
    31.     If oRs.BOF = False And oRs.EOF = False Then
    32.         oRs.MoveFirst
    33.         Do While oRs.EOF = False
    34.             List1.AddItem oRs.Fields("TABLE_NAME")
    35.             oRs.MoveNext
    36.             DoEvents
    37.         Loop
    38.     End If
    39.     Label1.Caption = "Sheets in workbook: " & _
    40.     Mid$(CommonDialog1.FileName, InStrRev(CommonDialog1.FileName, "\") + 1)
    41.     oCnn.Close
    42.     Set oCnn = Nothing
    43.     Exit Sub
    44. MyError:
    45.     MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbExclamation
    46.     If TypeName(oCnn) <> "Nothing" Then
    47.         If oCnn.State = adStateOpen Then oCnn.Close
    48.     End If
    49.     Set oCnn = Nothing
    50. End Sub
    51.  
    52. Private Sub cmdClose_Click()
    53.     Unload Me
    54. End Sub
    Last edited by RobDog888; Aug 19th, 2007 at 04:06 PM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [FAQ's: OD] How do I enumerate the sheets in a workbook without using Excel?

    VB 6 And Excel 2007 Code Example:
    VB Code:
    1. Option Explicit
    2. 'Copyright © 2006 by RobDog888 (VB/Office Guru™). All Rights reserved.
    3. '
    4. '              You may not reproduce or publish this
    5. '              code on any web site, online service,
    6. '              or distribute as source on any media
    7. '              without express permission.
    8.  
    9. 'Add a CommonDialog control to your form, 2 buttons and 1 label
    10. 'Add a reference to Microsoft ActiveX Data Objects 2.x Library
    11. Private Sub Command1_Click()
    12.     On Error GoTo MyError
    13.     Dim oCnn As ADODB.Connection
    14.     Dim oRs As ADODB.Recordset
    15.     Dim strFileName As String
    16.    
    17.     With CommonDialog1
    18.         .CancelError = True
    19.         .DialogTitle = "Select an Excel File."
    20.         .Filter = "Excel 2007 Format (*.xlsx)|*.xlsx"
    21.         .FilterIndex = 1
    22.         .Flags = cdlOFNFileMustExist Or cdlOFNPathMustExist
    23.         .ShowOpen
    24.         strFileName = .FileName
    25.     End With
    26.     Set oCnn = New ADODB.Connection
    27.     oCnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
    28.     strFileName & ";Extended Properties=""Excel 12.0;HDR=YES"";"
    29.     oCnn.Open
    30.     Set oRs = oCnn.OpenSchema(adSchemaTables)
    31.     If oRs.BOF = False And oRs.EOF = False Then
    32.         oRs.MoveFirst
    33.         Do While oRs.EOF = False
    34.             List1.AddItem oRs.Fields("TABLE_NAME")
    35.             oRs.MoveNext
    36.             DoEvents
    37.         Loop
    38.     End If
    39.     Label1.Caption = "Sheets in workbook: " & _
    40.     Mid$(CommonDialog1.FileName, InStrRev(CommonDialog1.FileName, "\") + 1)
    41.     oCnn.Close
    42.     Set oCnn = Nothing
    43.     Exit Sub
    44. MyError:
    45.     MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbExclamation
    46.     If TypeName(oCnn) <> "Nothing" Then
    47.         If oCnn.State = adStateOpen Then oCnn.Close
    48.     End If
    49.     Set oCnn = Nothing
    50. End Sub
    51.  
    52. Private Sub cmdClose_Click()
    53.     Unload Me
    54. End Sub
    Last edited by RobDog888; Aug 19th, 2007 at 04:07 PM. Reason: Fixed code tags from recent upgrade
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width