File Search Object

Use the FileSearch object to search for any file type based on various criteria. You can search for an exact filename, files of a specific type, or files containing a specific text located in a specific folder including or not including subfolders. The following function allows searching for a specific file or group of files. To search a specific folder, including subfolders, change the SearchSubFolders property to True. Any time you perform the new search, remember to reset the search settings using the NewSearch method. Note, however, that this method does not reset the LookIn property.

You can call the function shown below from a separate subroutine or by typing the following statement in the Immediate window:

Function FindFile(strFileName As String) Dim objFS As FileSearch Dim strFiles As String Dim varFile As Variant

Set objFS = Application.FileSearch With objFS .NewSearch .LookIn = "C:\" .Filename = strFileName .SearchSubFolders = False If .Execute() > 0 Then

For Each varFile In .FoundFiles strFiles = strFiles & varFile & vbCrLf Next varFile End If End With MsgBox strFiles End Function

Because the FileSearch object belongs to the Microsoft Office XP Object Model, it can be called from any Office XP application.

0 0

Post a comment