FileSearch Property

Returns a FileSearch object for use with file searches. Read-only.

Remarks

For more information, see FileSearch Object in the Microsoft Office Visual Basic Reference.

Example

This example searches the My Documents folder and any subfolders for the specified project. If only one project matching the search criterion is found, that project is opened; otherwise, a list of all matching projects is displayed.

Note: Before running this macro, change path to the path of your My Documents folder.

Sub FindMyProject()
    Dim TargetFile As String
    Dim MultiFiles As String
    Dim Count As Integer
    
    TargetFile = InputBox$("Enter the file name, including extension, of the project file you " & _
        "wish to open. You may use the wildcard characters * and ?.")
    
    With Application.FileSearch
        .LookIn = "path\My Documents"
        .SearchSubFolders = True
        .FileName = TargetFile
        
        If .Execute() = 1 Then
            Application.FileOpen .FoundFiles(1)
        
        ElseIf .Execute(msoSortByFileName, msoSortOrderAscending) > 1 Then
            For Count = 1 To .FoundFiles.Count
                MultiFiles = MultiFiles & .FoundFiles(Count) & vbCrLf
            Next Count
            
            MsgBox "More than one project was found. They are:" & vbCrLf & vbCrLf & MultiFiles
        
        ElseIf .Execute() = 0 Then
            MsgBox TargetFile & " could not be found."
        End If
    End With
End Sub

Applies to | Application Object