VBA Folder Dialog
The folder dialog is a dialog that prompts the user to select a directory path.
Jump To:
You can download the file and code for this file here. Please note that although the sample file is for MS Excel, it is still applicable to all other MS Office products.
Contents
Example:
In the example below the user is asked to select a directory path. If the user chooses a directory path the full path will be displayed in a message box:
Sub Example1()
Dim intResult As Integer
Dim strPath As String
'the dialog is displayed to the user
intResult = Application.FileDialog(msoFileDialogFolderPicker).Show
'checks if user has cancled the dialog
If intResult <> 0 Then
'dispaly message box
Call MsgBox(Application.FileDialog(msoFileDialogFolderPicker _
).SelectedItems(1), vbInformation, "Selected Folder")
End If
End Sub
The line below displays the folder dialog. If the user chooses to cancel the dialog, the value “0” will be assigned to intZero. Otherwise the value “-1” is assigned:
intResult = Application.FileDialog(msoFileDialogFolderPicker).Show
Result:
In the example above the folder “Folder to Start” in the directory “D:Temp is chosen. The full path is displayed in the message box below:
Button Name, ButtonName:
By default the folder dialog‘s action button’s name is “OK”, This could be changed to something more meaningful:
Sub Example2()
Dim intResult As Integer
Dim strPath As String
Application.FileDialog(msoFileDialogFolderPicker).ButtonName _
= "Select Path"
'the dialog is displayed to the user
intResult = Application.FileDialog(msoFileDialogFolderPicker).Show
'checks if user has cancled the dialog
If intResult <> 0 Then
'dispaly message box
Call MsgBox(Application.FileDialog(msoFileDialogFolderPicker _
).SelectedItems(1), _
vbInformation, "Selected Folder")
End If
End Sub
Result:
Change Title, Title:
The default title for the folder dialog is “Browse”:
The code below changes the default title to “Select a Path”:
Sub Example3()
Dim intResult As Integer
Dim strPath As String
'changes the folder dialogs title
Application.FileDialog(msoFileDialogFolderPicker).Title = _
"Select a Path"
'the dialog is displayed to the user
intResult = Application.FileDialog(msoFileDialogFolderPicker).Show
'checks if user has cancled the dialog
If intResult <> 0 Then
'dispaly message box
Call MsgBox(Application.FileDialog(msoFileDialogFolderPicker _
).SelectedItems(1), vbInformation, "Selected Folder")
End If
End Sub
Result:
Start Path, InitialFileName:
You can change the initial directory the folder dialog shows by using the InitialFileName property. The code below changes the initial directory the folder dialog shows to “D:TempFolder to Start”:
Sub Example4()
Dim intResult As Integer
Dim strPath As String
'changes the folder dialogs title
Application.FileDialog(msoFileDialogFolderPicker _
).InitialFileName = "D:TempFolder to Start"
'the dialog is displayed to the user
intResult = Application.FileDialog(msoFileDialogFolderPicker).Show
'checks if user has cancled the dialog
If intResult <> 0 Then
'dispaly message box
Call MsgBox(Application.FileDialog(msoFileDialogFolderPicker _
).SelectedItems(1), _
vbInformation, "Selected Folder")
End If
End Sub
Result:
You can download the file and code for this file here. Please note that although the sample file is for MS Excel, it is still applicable to all other MS Office products.
See also:
If you need assistance with your code, or you are looking for a VBA programmer to hire feel free to contact me. Also please visit my website www.software-solutions-online.com
2 thoughts on “VBA Folder Dialog”