Results 1 to 4 of 4

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

Threaded View

  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

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