FileDialogFilters 集合


表示文件对话框中可选文件类型的 FileDialogFilter 对象的集合,该对话框由 FileDialog 对象显示。

使用 FileDialogFilters 集合

使用 FileDialog 对象的 Filters 属性返回一个 FileDialogFilters 集合。下列代码返回“打开文件”对话框的 FileDialogFilters 集合。


使用 Add 方法向 FileDialogFilters 集合中添加 FileDialogFilter 对象。本示例使用 Clear 方法清除集合,然后向集合添加筛选。Clear 方法将集合完全清空,但是,如果在清除后没有向集合中添加任何筛选,将自动添加“所有文件”筛选。

Sub Main()

    'Declare a variable as a FileDialog object.
    Dim fd As FileDialog

    'Create a FileDialog object as a File Picker dialog box.
    Set fd = Application.FileDialog(msoFileDialogFilePicker)

    'Declare a variable to contain the path
    'of each selected item. Even though the path is a String,
    'the variable must be a Variant because For Each...Next
    'routines only work with Variants and Objects.
    Dim vrtSelectedItem As Variant

    'Use a With...End With block to reference the FileDialog object.
    With fd

        'Change the contents of the Files of Type list.
        'Empty the list by clearing the FileDialogFilters collection.

        'Add a filter that includes all files.
        .Filters.Add "All files", "*.*"

        'Add a filter that includes GIF and JPEG images and make it the first item in the list.
        .Filters.Add "Images", "*.gif; *.jpg; *.jpeg", 1

        'Use the Show method to display the File Picker dialog box and return the user's action.
        'The user pressed the action button.
        If .Show = -1 Then

            'Step through each String in the FileDialogSelectedItems collection.
            For Each vrtSelectedItem In .SelectedItems

                'vrtSelectedItem is a String that contains the path of each selected item.
                'You can use any file I/O functions that you want to work with this path.
                'This example simply displays the path in a message box.
                MsgBox "Path name: " & vrtSelectedItem

            Next vrtSelectedItem
        'The user pressed Cancel.
        End If
    End With

    'Set the object variable to Nothing.
    Set fd = Nothing

End Sub

在更改 FileDialogFilters 集合时,请记住每个应用程序只能实现一个 FileDialog 对象。这意味着在调用一个新对话框类型的 FileDialog 方法时,FileDialogFilters 集合将重置为默认筛选。

本示例在“另存为”对话框的默认筛选中迭代,并显示每个包括 Microsoft Excel 文件的筛选说明。

Sub Main()

    'Declare a variable as a FileDialogFilters collection.
    Dim fdfs As FileDialogFilters

    'Declare a variable as a FileDialogFilter object.
    Dim fdf As FileDialogFilter

    'Set the FileDialogFilters collection variable to
    'the FileDialogFilters collection of the SaveAs dialog box.
    Set fdfs = Application.FileDialog(msoFileDialogSaveAs).Filters

    'Iterate through the description and extensions of each
    'default filter in the SaveAs dialog box.
    For Each fdf In fdfs

        'Display the description of filters that include
        'Microsoft Excel files
        If InStr(1, fdf.Extensions, "xls", vbTextCompare) > 0 Then
            MsgBox "Description of filter: " & fdf.Description
        End If
    Next fdf

End Sub

注释 当应用于“另存为”FileDiaog 对象时,如果 Filters 属性与 ClearAddDelete 方法结合使用,将导致运行时出错。例如,Application.FileDialog(msoFileDialogSaveAs).Filters.Clear 将导致运行时出错。