Office VBA教程:FileTypes集合

   
FileSearch
FileTypes

msoFileType 类型值的集合,决定 FileSearch对象的 Execute方法返回的文件类型。

使用 FileTypes集合

使用 FileSearch对象的 FileTypes属性返回一个 FileTypes集合。例如:


Set ft = Application.FileSearch.FileTypes

注释    FileSearch对象的 FileType属性清除 FileTypes集合,并将集合中的第一项设置为 FileType属性定义的文件类型。

所有搜索只有一个 FileTypes集合,因此在执行搜索之前清除 FileTypes集合很重要,除非希望搜索上次搜索的文件类型。清除集合的最简便方法是将 FileType属性设置为要搜索的第一种文件类型。还可以使用 Remove方法删除单个类型。要确定集合中每项的文件类型,请使用 Item方法返回 msoFileType 值。

本示例搜索 C:\ 驱动器上的所有 HTML 和 Microsoft Excel 文件。


Sub SearchForFiles()
    'Declare a variable to act as a generic counter
    Dim lngCount As Long
    'Use a With...End With block to reference the
    'FileSearch object
    With Application.FileSearch
        'Clear all the parameters of the previous searches.
        'This method doesn't clear the LookIn property or
        'the SearchFolders collection.
        .NewSearch
        'Setting the FileType property clears the
        'FileTypes collection and sets the first
        'item in the collection to the file type
        'defined by the FileType property.
        .FileType = msoFileTypeWebPages
        'Add a second item to the FileTypes collection
        .FileTypes.Add msoFileTypeExcelWorkbooks
        'Display the number of FileTypes in the collection.
        MsgBox "You are about to search for " & .FileTypes.Count & _
            " file types."
        'Set up the search to look in all subfolders on the C:\ drive.
        .LookIn = "C:\"
        .SearchSubFolders = True
        'Execute the search and test to see if any files
        'were found.
        If .Execute <> 0 Then
            'Display the number of files found.
            MsgBox "Files found: " & .FoundFiles.Count
            'Loop through the list of found files and
            'display the path of each one in a message box.
            For lngCount = 1 To .FoundFiles.Count
                If MsgBox(.FoundFiles.Item(lngCount), vbOKCancel, _
                    "Found files") = vbCancel Then
                    'Break out of the loop
                    lngCount = .FoundFiles.Count
                End If
            Next lngCount
        Else
            MsgBox "No files found."
        End If
    End With
End Sub

本示例在 FileTypes集合中循环,并删除所有非 Microsoft Word 或 Microsoft Excel 文件的文件类型(通常,清除 FileTypes集合再从头开始更简便)。


Sub RemoveFileTypeFromCollection()
    'Define an integer to use as a counter
    'when iterating through the FileTypes collection.
    Dim intFileIndex As Integer
    'Use a With...End With block to reference the FileSearch object.
    With Application.FileSearch
        'Loop through all of the items in the FileTypes collection.
        intFileIndex = 1
        Do While intFileIndex <= .FileTypes.Count
            Select Case .FileTypes.Item(intFileIndex)
                Case msoFileTypeWordDocuments, msoFileTypeExcelWorkbooks
                Case Else
                    'If the file type isn't a Microsoft Word or
                    'Microsoft Excel file, remove it.
                    .FileTypes.Remove intFileIndex
                    'Decrement the counter so that no file types are missed.
                    intFileIndex = intFileIndex - 1
            End Select
            'Increment the counter to test the next file type.
            intFileIndex = intFileIndex + 1
        Loop
    End With
End Sub

上页:Office VBA教程:FileSearch对象 下页:Office VBA教程:FoundFiles对象

Office VBA教程:FileTypes集合

Office VBA教程:FoundFiles对象 Office VBA教程:HTMLProject对象
Office VBA教程:HTMLProjectItem对象 Office VBA教程:HTMLProjectItems集合对象
Office VBA教程:LanguageSettings对象 Office VBA教程:MsoEnvelope对象
Office VBA教程:NewFile对象 Office VBA教程:ODSOColumn对象
Office VBA教程:ODSOColumns对象 Office VBA教程:ODSOFilter对象
Office VBA教程:ODSOFilters对象 Office VBA教程:OfficeDataSourceObject对象
Office VBA教程:PropertyTest对象 Office VBA教程:PropertyTests集合对象
Office VBA教程:ScopeFolder对象 Office VBA教程:ScopeFolders集合
Office VBA教程:Script对象 Office VBA教程:Scripts集合对象
Office VBA教程:SearchFolders集合 Office VBA教程:SearchScope对象
版权所有 © 中山市飞娥软件工作室 证书:粤ICP备09170368号