Selecting a file using the FileDialog

In a Macro or Add-in there may be times when you require the user to select a file.  It is possible to use the standard FileDialog box for this purpose.  The following code is intended to provide the most common settings which you can amend to meet your needs.

Open the file DialogBox for selecting a file

Sub selectFile()

Dim dialogBox As FileDialog
Set dialogBox = Application.FileDialog(msoFileDialogOpen)

'Set the display properties - these are optional
'All the settings must be applied before the .Show command

'Do not allow multiple files to be selected
dialogBox.AllowMultiSelect = False

'Set the title of of the DialogBox
dialogBox.Title = "Select a file"

'Show the dialog box and output full file path and file name
If dialogBox.Show = -1 Then
   MsgBox "You selected: " & dialogBox.SelectedItems(1)
End If

End Sub

Selecting multiple files

If you the user is allowed to select more than one file the following code can be used:

Sub selectFiles()

Dim dialogBox As FileDialog
Set dialogBox = Application.FileDialog(msoFileDialogOpen)
Dim i As Integer
'Set the display properties - these are optional
'All the settings must be applied before the .Show command

'Allow multiple files to be selected
dialogBox.AllowMultiSelect = True

'Set the title of of the DialogBox
dialogBox.Title = "Select a file"

'Show the dialog box
'Loop through each selected item and output to Immediate Window
For i = 1 To dialogBox.SelectedItems.Count
    Debug.Print dialogBox.SelectedItems(i)
Next i

End Sub

Setting the default folder options

'Set the default folder to open
dialogBox.InitialFileName = "C:\Users\marks\Documents"

'Set filters to show specific files types only - these are optional
'Clear any existing filters first
dialogBox.Filters.Clear
'Add more filters - use ; to separate filters for the same name
dialogBox.Filters.Add "Excel workboooks", "*.xlsx;*.xls;*.xlsm"

File name or full file path

Sometimes you want the full file path of the selected item

'Show the full file path (folder & file name)
MgsBox dialogBox.SelectedItems(i)

Other times, you just want the name of the file (not the folder)

'Just show the file name
MsgBox Dir(dialogBox.SelectedItems(1))

Discover how you can automate your work with our Excel courses and tools.

Excel Academy

Excel Academy
The complete program for saving time by automating Excel.

Excel Automation Secrets

Excel Automation Secrets
Discover the 7-step framework for automating Excel.

Office Scripts Course

Office Scripts: Automate Excel Everywhere
Start using Office Scripts and Power Automate to automate Excel in new ways.

4 thoughts on “Selecting a file using the FileDialog”

  1. How do you open a single specific file with complete pathname to include filename already known using msoFileDialogOpen? No selection by the user, just open it immediately.

    Reply
  2. Every day I open the ‘open file’ dialog box in Excel, choose desktop and ‘all files’ (or text files), then choose my text file to import. I’ve tried everything I know to do.

    I can get this to open to desktop and with choosing text files or all files but clicking my daily (new) text file on my desktop doesn’t do anything. I need it to then go to the Text Import Wizard pop-up like I’m used to so that I can set things. How would I do that?

    Reply

Leave a Comment