FileDialogFilters 对象 (Office)

FileDialogFilter 对象的集合,这些对象代表可在使用 FileDialog 对象显示的文件对话框中选择的文件类型。


使用 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 aString, 
    '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 button. 
        If .Show = -1 Then 
            'Step through eachString in the FileDialogSelectedItems collection. 
            For Each vrtSelectedItem In .SelectedItems 
                'vrtSelectedItem is aString that contains the path of each selected item. 
                'Use any file I/O functions that you want to work with this path. 
                'This example 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 集合将重置为其默认筛选器。 下面的示例循环访问“SaveAs”对话框的默认筛选器,并显示每个包括 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


如果 Filters 属性与 ClearAddDelete 方法一起使用(应用于"另存为 FileDialog"对象时)将发生运行时错误。 例如, Application.FileDialog (msoFileDialogSaveAs) 。Filters.Clear 将导致运行时错误。



有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。