Share via


Objeto FileDialogFilters (Office)

Uma coleção de objetos FileDialogFilter que representam os tipos de arquivos que podem ser selecionados em uma caixa de diálogo de arquivo exibida usando o objeto FileDialog .

Exemplo

Use a propriedade Filters do objeto FileDialog para retornar uma coleção FileDialogFilters . O código a seguir retorna a coleção FileDialogFilters para a caixa de diálogo Abrir arquivo.

Application.FileDialog(msoFileDialogOpen).Filters

Use the Add method to add FileDialogFilter objects to the FileDialogFilters collection.

The following example uses the Clear method to clear the collection and then adds filters to the collection. O método Clear esvazia completamente a coleção; no entanto, se você não adicionar nenhum filtro à coleção depois de desmarcá-la, o filtro "Todos os arquivos (.)" será adicionado automaticamente.

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. 
        .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 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. 
        Else 
        End If 
    End With 
 
    'Set the object variable to Nothing. 
    Set fd = Nothing 
 
End Sub

Ao alterar a coleção FileDialogFilters, lembre-se de que cada aplicativo só pode criar uma instância de um único objeto FileDialog. Isso significa que a coleção FileDialogFilters redefine com seus filtros padrão sempre que você chamar o método FileDialog com um novo tipo de caixa de diálogo. O exemplo a seguir itera através dos filtros padrão da caixa de diálogo SaveAs e exibe a descrição de cada filtro que inclui um arquivo do 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

Observação

Ocorrerá um erro em tempo de execução se a propriedade Filters for usada em conjunto com os métodosLimpar, Adicionar ou Excluir quando aplicada a um objeto Save As FileDialog . Por exemplo, Application.FileDialog(msoFileDialogSaveAs). Filters.Clear resultará em um erro em tempo de execução.

Confira também

Suporte e comentários

Tem dúvidas ou quer enviar comentários sobre o VBA para Office ou sobre esta documentação? Confira Suporte e comentários sobre o VBA para Office a fim de obter orientação sobre as maneiras pelas quais você pode receber suporte e fornecer comentários.