Jon Carlstedt
Jon Carlstedt

Reputation: 2335

File selector, How do i specify filetype?

I have the following code that opens a file selector and lets the user pick a file.

Sub SelectFiles(ByRef test As String)

    Dim iFileSelect As FileDialog
    Set iFileSelect = Application.FileDialog(msoFileDialogFilePicker)

    Dim vrtSelectedItem As Variant  

        If iFileSelect.Show = -1 Then            

            For Each vrtSelectedItem In iFileSelect.SelectedItems
                test = vrtSelectedItem
            Next vrtSelectedItem

        End If

    Set iFileSelect = Nothing

End Sub

I would like it to only display files of a certain type (in this case XML) as it is now, the user can select any file type.

I have been through some creative Google searches but cant seem to find a solution and I have a feeling that it should be quite simple.

Upvotes: 7

Views: 38702

Answers (1)

user2140173
user2140173

Reputation:

Sub Main()
    Dim test As String
    SelectFiles test
    Debug.Print test
End Sub

Sub SelectFiles(ByRef test As String)

    Dim iFileSelect As FileDialog
    Set iFileSelect = Application.FileDialog(msoFileDialogOpen)
    With iFileSelect
        .AllowMultiSelect = True
        .Title = "Select XML Files"
        .Filters.Clear
        .Filters.Add "Extensible Markup Language Files", "*.xml"
        .InitialView = msoFileDialogViewDetails
        If .Show = -1 Then
            Dim vrtSelectedItem
            For Each vrtSelectedItem In iFileSelect.SelectedItems
                test = vrtSelectedItem
            Next vrtSelectedItem
        End If
    End With
    Set iFileSelect = Nothing
End Sub

Upvotes: 15

Related Questions