Importing all sheets in a spreadsheet into Access 2007

Pat
5StarLounger
Posts: 1148
Joined: 08 Feb 2010, 21:27

Importing all sheets in a spreadsheet into Access 2007

Post by Pat »

I have a requirement to import all sheets of a spreadsheet file into Access.
I have found the following code:

Code: Select all


' Replace 3 with the number of worksheets to be imported
' from each EXCEL file
For intWorksheets = 1 To 3

      strFile = Dir(strPath & "*.xls")
      Do While Len(strFile) > 0
            strPathFile = strPath & strFile
            DoCmd.TransferSpreadsheet acImport, _
                  acSpreadsheetTypeExcel9, strTables(intWorksheets), _
                  strPathFile, blnHasFieldNames, _
                  strWorksheets(intWorksheets) & "$"
            strFile = Dir()
      Loop

Next intWorksheets
What i need to obtain is the number of sheets and each sheet name, can some one help?

Pat
5StarLounger
Posts: 1148
Joined: 08 Feb 2010, 21:27

Re: Importing all sheets in a spreadsheet into Access 2007

Post by Pat »

I have found out how to do it by the following:

Code: Select all

' Open the EXCEL file and read the worksheet names into a collection
Set colWorksheets = New Collection
Set objWorkbook = objExcel.Workbooks.Open(strPathFile, , blnReadOnly, , _
      strPassword)
For lngCount = 1 To objWorkbook.Worksheets.Count
      colWorksheets.Add objWorkbook.Worksheets(lngCount).Name
Next lngCount