Often in VBA we need to ask the users to select files or directories before we execute the actual functionality of our macro. Welcome to the VBA Open file dialog post. Today we will learn how to use the Application.FileDialog, to understand the various msoFileDialogFilePicker file dialog picking options and how to properly manage these dialogs.
Here is a simple example of a VBA File Dialog:
Dim fDialog As FileDialog Set fDialog = Application.FileDialog(msoFileDialogFilePicker) 'Show the dialog. -1 means success! If fDialog.Show = -1 Then Debug.Print fDialog.SelectedItems(1) 'The full path to the file selected by the user End If
Application FileDialog function
Before we start let’s understand the Application.FileDialog function.
The Application.FileDialog has the following syntax:
Application.FileDialog( fileDialogType as MsoFileDialogType )
Parameter
MsoFileDialogType
An enumeration defining the type of file dialog to open. It has the following values:
Value | Description |
---|---|
msoFileDialogOpen | Open dialog box |
msoFileDialogSaveAs | Save As dialog box |
msoFileDialogFilePicker | File picker dialog box |
msoFileDialogFolderPicker | Folder picker dialog box |
Properties and functions
FileDialog properties
Property | Description |
---|---|
AllowMultiSelect | Allow to select more than one file or folder |
ButtonName | Text displayed on the action button of a file dialog box |
DialogType | Change the MsoFileDialogType (see above) |
Filter | Set a file filter to filter file types user can select |
InitialFileName | The initial path to be opened e.g. C:\ |
InitialView | The initial file view. Can be one of the following:
|
SelectedItems | Collection of type FileDialogSelectedItems with all selected items |
Title | Title of the Open file dialog window |
Select files – msoFileDialogFilePicker
The msoFileDialogFilePicker dialog type allows you to select one or more files.
Select single files
The most common select file scenario is asking the user to select a single file. The code below does just that:
Dim fDialog As FileDialog, result As Integer Set fDialog = Application.FileDialog(msoFileDialogFilePicker) 'Optional: FileDialog properties fDialog.AllowMultiSelect = False fDialog.title = "Select a file" fDialog.InitialFileName = "C:\" 'Optional: Add filters fDialog.Filters.Clear fDialog.Filters.Add "Excel files", "*.xlsx" fDialog.Filters.Add "All files", "*.*" 'Show the dialog. -1 means success! If fDialog.Show = -1 Then Debug.Print fDialog.SelectedItems(1) End If 'Result: C:\somefile.xlsx
Select multiple files
Quite common is a scenario when you are asking the user to select one or more files. The code below does just that. Notice that you need to set AllowMultiSelect to True.
Dim fDialog As FileDialog, result As Integer Set fDialog = Application.FileDialog(msoFileDialogFilePicker) 'IMPORTANT! fDialog.AllowMultiSelect = True 'Optional FileDialog properties fDialog.title = "Select a file" fDialog.InitialFileName = "C:\" 'Optional: Add filters fDialog.Filters.Clear fDialog.Filters.Add "Excel files", "*.xlsx" fDialog.Filters.Add "All files", "*.*" 'Show the dialog. -1 means success! If fDialog.Show = -1 Then For Each it In fDialog.SelectedItems Debug.Print it Next it End If 'Results: 'C:\somefile.xlsx 'C:\somefile1.xlsx 'C:\somefile2.xlsx
Select folder – msoFileDialogFilePicker
Selecting a folder is more simple than selecting files. However only a single folder can be select within a single dialog window.
Select folder example
The dialog below will ask the user to select a folder:
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker) 'Important we use msoFileDialogFolderPicker instead of (...)FilePicker 'Optional: Properties fDialog.title = "Select a folder" fDialog.InitialFileName = "C:\" If fDialog.Show = -1 Then Debug.Print fDialog.SelectedItems(1) End If
Open file – msoFileDialogOpen
Opening files is much more simple as it usually involves a single file. The only difference between the behavior between Selecting and Opening files are button labels.
Open file example
The dialog below will ask the user to select a file to open:
Dim fDialog As FileDialog, result As Integer, it As Variant Set fDialog = Application.FileDialog(msoFileDialogOpen) 'Optional: FileDialog properties fDialog.title = "Select a file" fDialog.InitialFileName = "C:\" 'Optional: Add filters fDialog.Filters.Clear fDialog.Filters.Add "All files", "*.*" fDialog.Filters.Add "Excel files", "*.xlsx" If fDialog.Show = -1 Then Debug.Print fDialog.SelectedItems(1) End If
Save file – msoFileDialogSaveAs
Saving a file is similarly easy, and also only the buttons are differently named.
Save file example
The dialog below will ask the user to select a path to which a files is to be saved:
Dim fDialog As FileDialog, result As Integer, it As Variant Set fDialog = Application.FileDialog(msoFileDialogSaveAs) 'Optional: FileDialog properties fDialog.title = "Save a file" fDialog.InitialFileName = "C:\" If fDialog.Show = -1 Then Debug.Print fDialog.SelectedItems(1) End If
FileDialog Filters
One of the common problems with working with the Application.FileDialog is setting multiple file filters. Below some common examples of how to do this properly. To add a filter for multiple files use the semicolor ;:
Dim fDialog As FileDialog Set fDialog = Application.FileDialog(msoFileDialogOpen) '... 'Optional: Add filters fDialog.Filters.Clear fDialog.Filters.Add "All files", "*.*" fDialog.Filters.Add "Excel files", "*.xlsx;*.xls;*.xlsm" fDialog.Filters.Add "Text/CSV files", "*.txt;*.csv" '...
Be sure to clear your list of filters each time. The FileDialog has its nuisances and often filters are not cleared automatically. Hence, when creating multiple dialogs you might see filters coming from previous executed dialogs if not cleared and re-initiated properly.