Share via


Creating Reusable File-Search Code

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

Searching for files is something you might do repeatedly in any number of different Office applications. This makes the FileSearch object a great candidate for encapsulation in a class module that could be used in any Microsoft® Office application that requires file-searching capabilities.

Dialog Box Used to Gather Custom Search Criteria

Aa140946.00602(en-us,office.10).gif

The Find Office Files dialog box is shown immediately after executing a search for files with an ".xls" extension in the "c:\my documents" directory. Code behind the Find Matching Files command button uses a global variable named objFileInfo to call the GetFileList method of the custom clsGetFileInfo class as follows:

Sub UpdateFileList()
   ' If the file-search specifications are valid, update
   ' the files contained in the form's combo box with a current
   ' list of matching files.
   
   Dim varFoundFiles   As Variant
   Dim varFile         As Variant
      
   varFoundFiles = objFileInfo.GetFileList
   
   If IsArray(varFoundFiles) Then
      With Me
         .cboMatchingFiles.Clear
         For Each varFile In varFoundFiles
            .cboMatchingFiles.AddItem varFile
         Next varFile
         .cboMatchingFiles.ListIndex = 0
         .lblFilesFound.Caption = CStr(objFileInfo.MatchingFilesFound) _
            & " Matching Files Found:"
      End With
   Else
      MsgBox "No files matched the specification: '" & Me.txtFileSpec & "'"
   End If
End Sub

The class contains several properties used to set the properties of the FileSearch object. It also exposes the GetFileList method that returns an array containing all files that match the specified criteria.

Public Function GetFileList() As Variant
   ' This function returns an array of files that match the criteria
   ' specified by the SearchPath and SearchName properties. If the
   ' SearchSubDirs property is set to True, the search includes
   ' subdirectories of SearchPath.

   Dim intFoundFiles    As Integer
   Dim astrFiles()      As String
   Dim fsoFileSearch    As FileSearch
   
   Set fsoFileSearch = Application.FileSearch
   With fsoFileSearch
      .NewSearch
      .LookIn = p_strPath
      .FileName = p_strName
      .FileType = msoFileTypeAllFiles
      .SearchSubFolders = p_blnSearchSubs
      
      If .Execute(p_intSortBy, p_intSortOrder) > 0 Then
         p_intFoundFiles = .FoundFiles.Count
         ReDim astrFiles(1 To .FoundFiles.Count)
            For intFoundFiles = 1 To .FoundFiles.Count
               astrFiles(intFoundFiles) = .FoundFiles(intFoundFiles)
            Next intFoundFiles
            GetFileList = astrFiles
      Else
         GetFileList = ""
      End If
   End With
End Function

See Also

Working with Shared Office Components | Working with the FileSearch Object | The Basics of File Searching | Using Advanced File-Searching Features | Custom Classes and Objects