Select folder names

 2000-02-04    Files & Folders    2    239

With the help of a couple of API-functions it's possible to display a dialog that lets the user select a folder:

Private Type BROWSEINFO ' used by the function GetFolderName
    hOwner As Long
    pidlRoot As Long
    pszDisplayName As String
    lpszTitle As String
    ulFlags As Long
    lpfn As Long
    lParam As Long
    iImage As Long
End Type

Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _
    Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32.dll" _
    Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long

Function GetFolderName(Msg As String) As String
' returns the name of the folder selected by the user
Dim bInfo As BROWSEINFO, path As String, r As Long
Dim X As Long, pos As Integer
    bInfo.pidlRoot = 0& ' Root folder = Desktop
    If IsMissing(Msg) Then
        bInfo.lpszTitle = "Select a folder." 
        ' the dialog title
        bInfo.lpszTitle = Msg ' the dialog title
    End If
    bInfo.ulFlags = &H1 ' Type of directory to return
    X = SHBrowseForFolder(bInfo) ' display the dialog
    ' Parse the result
    path = Space$(512)
    r = SHGetPathFromIDList(ByVal X, ByVal path)
    If r Then
        pos = InStr(path, Chr$(0))
        GetFolderName = Left(path, pos - 1)
        GetFolderName = ""
    End If
End Function

Sub TestGetFolderName()
Dim FolderName As String
    FolderName = GetFolderName("Select a folder")
    If FolderName = "" Then
        MsgBox "You didn't select a folder."
        MsgBox "You selected this folder: " & FolderName
    End If
End Sub

Leave a comment:

Your comment will only be published after it has been moderated and found spam free.
Your e-mail address will only be used to display your Gravatar.

OPE | 2010-05-23 10:47:01 (GMT)

I don't think it is possible to have multiselection of folders, I am not aware of any tool made specifically for this task.
In the mean time you can select single folders and add them to e.g. a listbox control so that you can work with this information later in your macro.

Gastón Caino | 2010-05-20 15:38:58 (GMT)

Hello master!
how i can have a multiseleccion?,
i need that user can select several folders at once, and i take this paths and then can work with them.