Microsoft Office 2000/Visual Basic Programmer's Guide   

Using Advanced File-Searching Features

You get programmatic access to the advanced features of the FileSearch object by using its PropertyTests collection. These features correspond to the options available in the Advanced Find dialog box, which is available through the Office File Open dialog box.

Figure 6.1 The Advanced Find Dialog Box

The PropertyTests collection contains the criteria for a file search. Some of these criteria may have been specified by properties of the FileSearch object itself while others must be added to the PropertyTests collection by using its Add method.

In the following example, one of the file-search criteria added to the PropertyTests collection corresponds to the Contents setting in the Property box and another corresponds to the includes words setting in the Condition box in Figure 6.1.

Set fsoFileSearch = Application.FileSearch
With fsoFileSearch
   .NewSearch
   .FileName = strFileName
   .LookIn = strLookIn
   .SearchSubFolders = blnSearchSubDir
   .PropertyTests.Add "Contents", msoConditionIncludes, strFindThisText
   If .Execute(msoSortByFileName, msoSortOrderAscending, True) > 0 Then
      For Each varFile In .FoundFiles
         cboFoundCombo.AddItem varFile
      Next varFile
   Else
      cboFoundCombo.AddItem "No Matching Files Located!"
   End If
   cboFoundCombo.ListIndex = 0
   Beep
   Beep
   Beep
End With

The preceding code fragment is part of the FindFiles procedure, which is available in the modAdvancedFileSearchObject module in FileSearch.xls in the ODETools\V9\Samples\OPG\Samples\CH06 subfolder on the Office 2000 Developer CD-ROM.