Read Excel file sheet names

后端 未结 2 1556
天命终不由人
天命终不由人 2021-01-25 08:02

I have an export process that transfers data from my Access tables to an Excel File. A couple times I have had issues where the process didn\'t generate one or more of the sheet

相关标签:
2条回答
  • 2021-01-25 08:22

    From Access you can automate Excel, open the workbook file, and read the sheet names from the Worksheets collection.

    This sample uses late binding. If you prefer early binding, add a reference for Microsoft Excel [version] Object Library and enable the "early" lines instead of the "late" lines.

    Give the procedure the full path to your workbook file as its pWorkBook parameter.

    Public Sub List_worksheets(ByVal pWorkBook As String)
        'Dim objExc As Excel.Application ' early
        'Dim objWbk As Excel.Workbook ' early
        'Dim objWsh As Excel.Worksheet ' early
        Dim objExc As Object ' late
        Dim objWbk As Object ' late
        Dim objWsh As Object ' late
    
        'Set objExc = New Excel.Application ' early
        Set objExc = CreateObject("Excel.Application") ' late
        Set objWbk = objExc.Workbooks.Open(pWorkBook)
        For Each objWsh In objWbk.Worksheets
            Debug.Print objWsh.Name
        Next
        Set objWsh = Nothing
        objWbk.Close
        Set objWbk = Nothing
        objExc.Quit
        Set objExc = Nothing
    End Sub
    
    0 讨论(0)
  • 2021-01-25 08:30

    In Access 2007, You can use OpenDatabase method to do this:

    Private Sub Command1_Click()
    Set db = OpenDatabase("c:/123.xls", True, False, "Excel 5.0")
        For Each tbl In db.TableDefs
            MsgBox tbl.Name
        Next
    End Sub
    
    0 讨论(0)
提交回复
热议问题