P
pietlinden
Sorry, I must be having a blonde moment...
I'm looping through a directory and processing all the Excel files in
the directory. What I don't understand is why I'm getting weird errors
when I run the following loop:
Private Sub Command4_Click()
Dim strFolder As String
Dim strFile As String
'--allow user to select folder to process...
strFolder = BrowseFolder("Select a folder")
'--loop through all Excel files in the directory
strFile = Dir(strFolder & "\*.XLS")
Do While Len(strFile) > 0
MsgBox strFile
'---for some reason, this works great exactly twice.
GetFieldNames strFolder & "\" & strFile
strFile = Dir
Loop
End Sub
Public Sub GetFieldNames(ByVal strFile)
' pass in the result of the function that gets the list of files.
On Error GoTo ErrHandler:
Dim appXL As Excel.Application
Dim intCounter As Integer
Dim rs As DAO.Recordset
Set appXL = New Excel.Application
appXL.Workbooks.Open strFile
Set rs = DBEngine(0)(0).OpenRecordset("tblExcelColumns",
dbOpenTable, dbAppendOnly)
For intCounter = 1 To
appXL.ActiveWorkbook.Worksheets(1).Columns.Count
If Not IsEmpty(ActiveWorkbook.Worksheets(1).Cells(1,
intCounter)) Then
'---Change this line to write the column name and the
workbook name to a recordset
rs.AddNew
rs.Fields("FileName") = ActiveWorkbook.Path & "\" &
ActiveWorkbook.Name
rs.Fields("ColumnName") =
ActiveWorkbook.Worksheets(1).Cells(1, intCounter)
rs.Update
Else
Exit For
End If
Next intCounter
MsgBox intCounter & " records hopefully written!"
rs.Close
Set rs = Nothing
appXL.Workbooks.Close
'---I *SHOULD* use something better for this, but right now I'm
worrying about the looping mess...
appXL.Quit
Set appXL = Nothing
Exit Sub
ErrHandler:
rs.Close
Set rs = Nothing
appXL.Workbooks.Close
appXL.Quit
Set appXL = Nothing
End Sub
What's weird is that the loop works great exactly twice. I must be
doing something wrong with DIR. Any clues?
Thanks!
I'm looping through a directory and processing all the Excel files in
the directory. What I don't understand is why I'm getting weird errors
when I run the following loop:
Private Sub Command4_Click()
Dim strFolder As String
Dim strFile As String
'--allow user to select folder to process...
strFolder = BrowseFolder("Select a folder")
'--loop through all Excel files in the directory
strFile = Dir(strFolder & "\*.XLS")
Do While Len(strFile) > 0
MsgBox strFile
'---for some reason, this works great exactly twice.
GetFieldNames strFolder & "\" & strFile
strFile = Dir
Loop
End Sub
Public Sub GetFieldNames(ByVal strFile)
' pass in the result of the function that gets the list of files.
On Error GoTo ErrHandler:
Dim appXL As Excel.Application
Dim intCounter As Integer
Dim rs As DAO.Recordset
Set appXL = New Excel.Application
appXL.Workbooks.Open strFile
Set rs = DBEngine(0)(0).OpenRecordset("tblExcelColumns",
dbOpenTable, dbAppendOnly)
For intCounter = 1 To
appXL.ActiveWorkbook.Worksheets(1).Columns.Count
If Not IsEmpty(ActiveWorkbook.Worksheets(1).Cells(1,
intCounter)) Then
'---Change this line to write the column name and the
workbook name to a recordset
rs.AddNew
rs.Fields("FileName") = ActiveWorkbook.Path & "\" &
ActiveWorkbook.Name
rs.Fields("ColumnName") =
ActiveWorkbook.Worksheets(1).Cells(1, intCounter)
rs.Update
Else
Exit For
End If
Next intCounter
MsgBox intCounter & " records hopefully written!"
rs.Close
Set rs = Nothing
appXL.Workbooks.Close
'---I *SHOULD* use something better for this, but right now I'm
worrying about the looping mess...
appXL.Quit
Set appXL = Nothing
Exit Sub
ErrHandler:
rs.Close
Set rs = Nothing
appXL.Workbooks.Close
appXL.Quit
Set appXL = Nothing
End Sub
What's weird is that the loop works great exactly twice. I must be
doing something wrong with DIR. Any clues?
Thanks!