Office VBA教程: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属性与 Clear、Add 或 Delete方法结合使用,将导致运行时出错。例如,Application.FileDialog(msoFileDialogSaveAs).Filters.Clear 将导致运行时出错。

上页:Office VBA教程:FileDialogFilter对象 下页:Office VBA教程:FileDialogSelectedItems集合

Office VBA教程:FileDialogFilters集合

Office VBA教程:FileDialogSelectedItems集合 Office VBA教程:FileSearch对象
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对象
版权所有 © 中山市飞娥软件工作室 证书:粤ICP备09170368号