I am trying to modify the following code from "Ken's Examples for Importing from EXCEL Workbook Files" in an Access 2007 database.
I need to cycle thru all the workbooks in c:\temp , open them and determine if any worksheets are name *child* "look for the word child which will have chars before and after the word and import only the worksheets that have the word child in them to an existing table in Access 2007. I would also like to find the worksheets that have the name *parent" in them and export those to another existing table in Access 2007. Not sure how to make this work correctly or if it can even be done.
Here's what I have so far but I get an error message saying the object does not support this property or method.
Thanks in advance for some direction!
Function ImportData()
Dim blnHasFieldNames As Boolean, blnEXCEL As Boolean, blnReadOnly As Boolean
Dim intWorkbookCounter As Integer
Dim lngCount As Long
Dim objExcel As Object, objWorkbook As Object
Dim colWorksheets As Collection
Dim strPath As String, strFile As String
Dim strPassword As String
' Establish an EXCEL application object
On Error Resume Next
Set objExcel = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Set objExcel = CreateObject("Excel.Application")
blnEXCEL = True
End If
Err.Clear
On Error GoTo 0
blnHasFieldNames = True
strPath = "C:\temp\"
strPassword = vbNullString
blnReadOnly = True
strFile = Dir(strPath & "*.xls")
intWorkbookCounter = 0
Do While strFile <> ""
intWorkbookCounter = intWorkbookCounter + 1
Set colWorksheets = New Collection
Set objWorkbook = objExcel.Workbooks.Open(strPath & strFile, , _
blnReadOnly, , strPassword)
For lngCount = 1 To objWorkbook.Worksheets.Count
If objWorkbook.Worksheets.Name = "*child*" Then ' would like this line to look for the word child in the acutal worksheet name
colWorksheets.Add objWorkbook.Worksheets(lngCount).Name
End If
Next lngCount
' Close the EXCEL file without saving the file, and clean up the EXCEL objects
objWorkbook.Close False
Set objWorkbook = Nothing
' Import the data from each worksheet into a separate table
For lngCount = colWorksheets.Count To 1 Step -1
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
"tbl" & colWorksheets(lngCount) & intWorkbookCounter, _
strPath & strFile, blnHasFieldNames, _
colWorksheets(lngCount) & "$"
Next lngCount
' Delete the collection
Set colWorksheets = Nothing
strFile = Dir()
Loop
If blnEXCEL = True Then objExcel.Quit
Set objExcel = Nothing
End Function
I need to cycle thru all the workbooks in c:\temp , open them and determine if any worksheets are name *child* "look for the word child which will have chars before and after the word and import only the worksheets that have the word child in them to an existing table in Access 2007. I would also like to find the worksheets that have the name *parent" in them and export those to another existing table in Access 2007. Not sure how to make this work correctly or if it can even be done.
Here's what I have so far but I get an error message saying the object does not support this property or method.
Thanks in advance for some direction!
Function ImportData()
Dim blnHasFieldNames As Boolean, blnEXCEL As Boolean, blnReadOnly As Boolean
Dim intWorkbookCounter As Integer
Dim lngCount As Long
Dim objExcel As Object, objWorkbook As Object
Dim colWorksheets As Collection
Dim strPath As String, strFile As String
Dim strPassword As String
' Establish an EXCEL application object
On Error Resume Next
Set objExcel = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Set objExcel = CreateObject("Excel.Application")
blnEXCEL = True
End If
Err.Clear
On Error GoTo 0
blnHasFieldNames = True
strPath = "C:\temp\"
strPassword = vbNullString
blnReadOnly = True
strFile = Dir(strPath & "*.xls")
intWorkbookCounter = 0
Do While strFile <> ""
intWorkbookCounter = intWorkbookCounter + 1
Set colWorksheets = New Collection
Set objWorkbook = objExcel.Workbooks.Open(strPath & strFile, , _
blnReadOnly, , strPassword)
For lngCount = 1 To objWorkbook.Worksheets.Count
If objWorkbook.Worksheets.Name = "*child*" Then ' would like this line to look for the word child in the acutal worksheet name
colWorksheets.Add objWorkbook.Worksheets(lngCount).Name
End If
Next lngCount
' Close the EXCEL file without saving the file, and clean up the EXCEL objects
objWorkbook.Close False
Set objWorkbook = Nothing
' Import the data from each worksheet into a separate table
For lngCount = colWorksheets.Count To 1 Step -1
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
"tbl" & colWorksheets(lngCount) & intWorkbookCounter, _
strPath & strFile, blnHasFieldNames, _
colWorksheets(lngCount) & "$"
Next lngCount
' Delete the collection
Set colWorksheets = Nothing
strFile = Dir()
Loop
If blnEXCEL = True Then objExcel.Quit
Set objExcel = Nothing
End Function