This is some code I wrote the other day to retrieve info from form fields on word docs saved in a particular folder which avoids using filesearch. It may be of use to you:
Sub Log_Docs()
Dim WdApp As Object, WdDoc As Object, blnAppOpen As Boolean, strDocName As String
Dim lngWriteRow As Long, strDir As String
blnAppOpen = True
strDir = "C:\Returned Docs\"
If MsgBox("Data will be retrieved from all Word documents in this folder:" & vbCrLf & vbCrLf & _
strDir & vbCrLf & vbCrLf & "Do you want to continue?", vbYesNo + vbQuestion) = vbNo Then
Exit Sub
End If
' Look for first word document
strDocName = Dir(strDir & "*.doc")
' If no word document found then exit
If strDocName = "" Then
MsgBox "No files found!", vbCritical
Exit Sub
End If
On Error GoTo ErrorHandler
' Get open instance of word application if available
Set WdApp = GetObject(, "Word.Application")
Do While strDocName <> ""
' Open word document
Set WdDoc = WdApp.Documents.Open(strDir & strDocName)
With Sheets("Sheet1")
' Find last completed row on reporting log
lngWriteRow = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
' Populate worksheet
.Range("A" & lngWriteRow).Value = WdDoc.FormFields("Text1").Result
.Range("B" & lngWriteRow).Value = WdDoc.FormFields("Text2").Result
.Range("C" & lngWriteRow).Value = WdDoc.FormFields("Dropdown1").Result
.Range("D" & lngWriteRow).Value = strDocName
.Range("E" & lngWriteRow).Value = Now
End With
' Close word document and move to Logged folder
WdDoc.Close SaveChanges:=False
Name strDir & strDocName As strDir & "Logged/" & strDocName
' Look for next word document
strDocName = Dir
Loop
Set WdDoc = Nothing
If blnAppOpen = False Then
WdApp.Quit
Set WdApp = Nothing
End If
Exit Sub
' Error Handler
ErrorHandler:
If Err.Number = 429 Then
'Word is not running, open Word with CreateObject
Set WdApp = CreateObject("Word.Application")
blnAppOpen = False
Resume Next
Else
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
End If
End Sub
You will need to set references to the Miscrosoft Forms and Microsoft Word object libraries in the Excel VBA editor.
Dom
Bookmarks