Colección FileDialogFilters

Microsoft Office Objects

Colección FileDialogFilters

         
FileDialog FileDialogFilters
FileDialogFilter

Colección de objetos FileDialogFilter que representa los tipos de archivos que pueden seleccionarse en el cuadro de diálogo de archivos que se muestra utilizando el objeto FileDialog.

Utilizar la colección FileDialogFilters

Utilice la propiedad Filters del objeto FileDialog para devolver una colección FileDialogFilters. El código siguiente devuelve la colección FileDialogFilters para el cuadro de diálogo Abrir archivo.

Application.FileDialog(msoFileDialogOpen).Filters

Utilice el método Add para agregar objetos FileDialogFilter a la colección FileDialogFilters. El ejemplo siguiente utiliza el método Clear para borrar la colección y, a continuación, agregar filtros a la misma. El método Clear vacía completamente la colección; sin embargo, si no agrega filtros después de borrarla, el filtro "All files (*.*)" se agregará automáticamente.

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.
        .Filters.Clear

        '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.
        Else
        End If
    End With

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

End Sub

Al cambiar la colección FileDialogFilters recuerde que cada aplicación sólo puede repetir un solo objeto FileDialog. Esto significa que la colección FileDialogFilters restablecerá los filtros predeterminados al activar el método FileDialog con un tipo de cuadro de diálogo nuevo.

El ejemplo siguiente repite los filtros predeterminados del cuadro de diálogo Guardar como y muestra la descripción de cada filtro que incluye un archivo de 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

Nota  Se producirá un error en el tiempo de ejecución si utiliza la propiedad Filters junto con los métodos Clear, Add o Delete si se aplica a un objeto Save As FileDialog. Por ejemplo, Application.FileDialog(msoFileDialogSaveAs).Filters.Clear producirá un error de tiempo de ejecución.