FoundFiles 对象

         
FileSearch
PropertyTests (PropertyTest)
FoundFiles

代表由文件查找过程返回的文件列表。

使用 FoundFiles 对象

FoundFiles 属性可返回 FoundFiles 对象。本示例可实现:逐个查看找到的文件列表中的文件并显示其中每个文件的文件名和路径。用 FoundFiles(index) 可返回查找过程中指定文件的名称和位置,此处的 index 是该文件的索引号。

With Application.FileSearch
    For i = 1 To .FoundFiles.Count
        MsgBox .FoundFiles(i)
    Next I
End With

Execute 方法可开始文件查找过程,并更新 FoundFiles 对象。以下示例可实现:在“My Documents”文件夹中查找所有文件名以“Cmd”开头的文件,并显示找到的每个文件的位置和文件名。该示例还可实现:对返回的文件按文件名的字母升序排序。

Set fs = Application.FileSearch
With fs
    .LookIn = "C:\My Documents"
    .FileName = "cmd*"
    If .Execute(SortBy:=msoSortbyFileName, _
    SortOrder:=msoSortOrderAscending) > 0 Then
        MsgBox "There were " & .FoundFiles.Count & _
            " file(s) found."
        For i = 1 To .FoundFiles.Count
            MsgBox .FoundFiles(i)
        Next i
    Else
        MsgBox "There were no files found."
    End If
End With