Does anyone know of a way to retrieve the names of the worksheets from a workbook and use them as the source for a list box?
I have looked in the forums and even googled it but cant find a solution.
Ive got a form and on this form there is a button which a user presses to start the import of a spreadsheet, but the workbook contains quite a few spreadsheets and not all of the sheets are imported. So at present the user has to open the book, select the right sheet, copy its name and paste into a field on the import form then click the button, but Id prefer the user to select the worksheet name from a list box.
Ive managed to come up with the code below based on a function by SkipVought. But it wont work, all it does is open the book then close it. the sheet names are not obtained.
can anyone help?
cheers
Private Sub btnButton_Click()
Dim Filename As String
Dim objXL As Excel.Application
Dim objWkb As Excel.Workbook
Dim objSht As Excel.Worksheet
Dim db As DAO.Database
Dim shtName As String
Dim wkbName As String
Filename = ""
Filename = Dir(DBPath() & "*.xls")
wkbName = DBPath() & Filename
Set db = CurrentDb
Set objXL = New Excel.Application
Set objWkb = .Workbooks.Open(wkbName)
With objXL
.Visible = True
For Each objWkb In Workbooks
For Each objSht In Workbooks
DoCmd.RunSQL "INSERT INTO tblSheetList ( SheetName ) SELECT objSht.Name AS Expr1;"
Next
Next
objWkb.Close True
Set objSht = Nothing
Set objWkb = Nothing
objXL.Quit ' NOTE - changed sequence
Set objXL = Nothing
Set db = Nothing
End With
End Sub
"Children are smarter than any of us. Know how I know that? I don't know one child with a full time job and children."...Bill Hicks
I have looked in the forums and even googled it but cant find a solution.
Ive got a form and on this form there is a button which a user presses to start the import of a spreadsheet, but the workbook contains quite a few spreadsheets and not all of the sheets are imported. So at present the user has to open the book, select the right sheet, copy its name and paste into a field on the import form then click the button, but Id prefer the user to select the worksheet name from a list box.
Ive managed to come up with the code below based on a function by SkipVought. But it wont work, all it does is open the book then close it. the sheet names are not obtained.
can anyone help?
cheers
Private Sub btnButton_Click()
Dim Filename As String
Dim objXL As Excel.Application
Dim objWkb As Excel.Workbook
Dim objSht As Excel.Worksheet
Dim db As DAO.Database
Dim shtName As String
Dim wkbName As String
Filename = ""
Filename = Dir(DBPath() & "*.xls")
wkbName = DBPath() & Filename
Set db = CurrentDb
Set objXL = New Excel.Application
Set objWkb = .Workbooks.Open(wkbName)
With objXL
.Visible = True
For Each objWkb In Workbooks
For Each objSht In Workbooks
DoCmd.RunSQL "INSERT INTO tblSheetList ( SheetName ) SELECT objSht.Name AS Expr1;"
Next
Next
objWkb.Close True
Set objSht = Nothing
Set objWkb = Nothing
objXL.Quit ' NOTE - changed sequence
Set objXL = Nothing
Set db = Nothing
End With
End Sub
"Children are smarter than any of us. Know how I know that? I don't know one child with a full time job and children."...Bill Hicks