Use the function below to display a file picker window and let the user pick one or more files in the file system, depending on the parameters passed. Returns an array of all the complete paths to the files selected by the user, or nothing if the user didn’t select any files.

Function

VBA
Function UseFileDialogOpen(multiSelect As Boolean, Optional strTitle As String, _ Optional strFilterName As String, Optional strFilters As String, Optional strInitPath As String) As Variant Dim i As Long, lngCount As Long Dim res() As Variant With Application.FileDialog(msoFileDialogOpen) If multiSelect Then .AllowMultiSelect = True If strTitle <> vbNullString Then .Title = strTitle If strFilterName <> vbNullString And strFilters <> vbNullString Then .Filters.Add strFilterName, strFilters, 1 If strInitPath <> vbNullString Then .InitialFileName = strInitPath .Show lngCount = .SelectedItems.Count If lngCount > 0 Then ReDim res(1 To lngCount) As Variant For i = 1 To .SelectedItems.Count res(i) = .SelectedItems(i) Next i UseFileDialogOpen = res End If End With End Function

Parameters

NameOptional/
Required
Data typeDescription
multiSelectRequiredBooleanDefault value is false. Specifies whether the user should be allowed to pick multiple files, or not.
strTitleOptionalStringThe name of the file picker window shown in the top left-hand corner.
strFilterNameOptionalStringThe name of the file filter, e.g. “Word files”. Has no effect if strFilters is not passed.
strFiltersOptionalStringSpecification of the allowed file types, e.g. “*.doc; *.docx”. Has no effect if strFilterName is not passed.
strInitPathOptionalStringThe path of the folder which the file picker window should open, e.g. “C:\MyFiles\”.

Usage example

VBA
Sub TestUseFileDialogOpen() Dim res As Variant, vItem As Variant res = UseFileDialogOpen(True, "Select one or more files", "File type", "*.doc; *.docx""", "c:\temp") ' Allow only MS Word .doc and .docx file types If Not IsArray(res) Then Exit Sub For Each vItem In res Debug.Print vItem Next vItem End Sub