Another useful code snippet for VBA:
The following function displays a dialog box that enables the user to select a folder and then returns a string containing the selected folder's path in a string.
Function SelectFolder(Optional i_RootFolder As String) As String
Dim myShell As Object
Dim myFolder As Object
Set myShell = CreateObject("Shell.Application")
If i_RootFolder = "" Then
'no root folder given, use default (which is Desktop)
Set myFolder = myShell.BrowseForFolder(0, "Please select a folder:", 1)
ElseIf Not (i_RootFolder Like "*[!0123456789]*") Then
'number for special folder given
Set myFolder = myShell.BrowseForFolder(0, _
"Please select a folder:", 1, CInt(i_RootFolder))
Else
'path for root folder given
Set myFolder = myShell.BrowseForFolder(0, _
"Please select a folder:", 1, CStr(i_RootFolder))
End If
If Not myFolder Is Nothing Then
SelectFolder = myFolder.Self.Path
End If
End Function
i_RootFolder is the root folder to use in the dialog box. The user cannot browse higher in the tree than this folder. If this value is not specified, the root folder used in the dialog box is the Desktop. This value can be a string that specifies the path of the folder or one of the
ShellSpecialFolderConstants values.