File Search code

  • Thread starter Thread starter Abdul Salam
  • Start date Start date
With Application.FileSearch
.NewSearch
.LookIn = "C:\My Documents"
.SearchSubFolders = True
.FileName = "Run"
' .MatchTextExactly = True
.FileType = msoFileTypeExcelWorkbooks
If .Execute() > 0 Then
For i = 1 To .FoundFiles.Count
if lcase(Right(.FoundFiles(i),7))="\run.xls" then
workbooks.open .foundfiles(i)
exit for
Next i
Else
MsgBox "file not found."
End If
End With

MatchTextExactly only pertains to searching for a word in a document - does
not pertain to the file name.


Regards,
Tom Ogilvy
 
Note that there was an omitted End If in the original - corrected here and
generalized.

Sub tester1()
Dim sName As String
Dim i As Long
sName = "run.xls"
With Application.FileSearch
.NewSearch
.LookIn = "C:\My Documents"
.SearchSubFolders = True
.Filename = sName
' .MatchTextExactly = True
.FileType = msoFileTypeExcelWorkbooks
If .Execute() > 0 Then
For i = 1 To .FoundFiles.Count
If LCase(Right(.FoundFiles(i), _
Len(sName) + 1)) = "\" _
& LCase(sName) Then
Workbooks.Open .FoundFiles(i)
Exit For
End If
Next i
Else
MsgBox "file not found."
End If
End With
End Sub
 
Back
Top