In the code below (and in several previous variations of) I keep getting a value of 0 for .Execute and .FoundFiles. I have had several experiences where it has correctly returned the number of files in the folder one day, and zero the next, even though there have always been files in the folder. The code returns the correct folder name so I know it is searching in the correct location, it just doesn't see the files that I know are there. Why don't I consistently get the correct number of files returned?
Sub Execute_Table_Load()
Dim File_Path As String
Dim Folder_Path As String
Dim i as Integer
Dim Document_List As String
Dim docName as String
Dim fs As FileSearch
File_Path = ThisWorkbook.Path
'Build a path to the folder where the word documents are:
Folder_Path = File_Path & "\Test"
Set fs = Application.FileSearch
With fs
.NewSearch
.LookIn = Folder_Path
.SearchSubFolders = False
.FileType = msoFileTypeWordDocuments
If .Execute > 0 Then
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
For i = 1 To .FoundFiles.Count
Set wrdDoc = wrdApp.Documents.Open(.FoundFiles(i))
docName = Left(wrdDoc.Name, Len(wrdDoc.Name) - 4)
'rest of code goes here
Document_List = Document_List & vbNewLine & docName
Next i
MsgBox "Data from these files were extracted:" & vbNewLine & Document_List
wrdApp.Quit 'close the Word application
Else
MsgBox "No Documents were found in folder: " & Folder_Path
End If
Set wrdApp = Nothing 'Explicitly clear memory
End With
End Sub
Sub Execute_Table_Load()
Dim File_Path As String
Dim Folder_Path As String
Dim i as Integer
Dim Document_List As String
Dim docName as String
Dim fs As FileSearch
File_Path = ThisWorkbook.Path
'Build a path to the folder where the word documents are:
Folder_Path = File_Path & "\Test"
Set fs = Application.FileSearch
With fs
.NewSearch
.LookIn = Folder_Path
.SearchSubFolders = False
.FileType = msoFileTypeWordDocuments
If .Execute > 0 Then
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
For i = 1 To .FoundFiles.Count
Set wrdDoc = wrdApp.Documents.Open(.FoundFiles(i))
docName = Left(wrdDoc.Name, Len(wrdDoc.Name) - 4)
'rest of code goes here
Document_List = Document_List & vbNewLine & docName
Next i
MsgBox "Data from these files were extracted:" & vbNewLine & Document_List
wrdApp.Quit 'close the Word application
Else
MsgBox "No Documents were found in folder: " & Folder_Path
End If
Set wrdApp = Nothing 'Explicitly clear memory
End With
End Sub