Here is the smart, Windows Explorer enabled directory picker dialog.
Add a code module and paste the following code in it.
-----------------------------------------------------------
Option Explicit
Const MAX_PATH = 260
Const BIF_RETURNONLYFSDIRS = &H1&
Const BIF_STATUSTEXT = &H4
Const WM_USER = &H400
Const BFFM_INITIALIZED = 1
Const BFFM_SELCHANGED = 2
Const BFFM_SETSTATUSTEXT = WM_USER + 100
Const BFFM_SETSELECTION = WM_USER + 102
Const SHGFI_DISPLAYNAME = &H200
Const SHGFI_PIDL = &H8
Const WM_SETTEXT = &HC
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Declare Function BrowseForFolder Lib "shell32" Alias "SHBrowseForFolder" (lpbi As BROWSEINFO) As Long
Declare Function GetPathFromIDList Lib "shell32" Alias "SHGetPathFromIDList" (ByVal pidl As Long, ByVal pszPath As String) As Long
Declare Function SHGetFileInfo Lib "shell32" Alias "SHGetFileInfoA" (ByVal pszPath As Any, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, ByVal cbFileInfo As Long, ByVal uFlags As Long) As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Type BROWSEINFO
hwndOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As String
iImage As Long
End Type
Type SHFILEINFO
hIcon As Long
iIcon As Long
dwAttributes As Long
szDisplayName As String * MAX_PATH
szTypeName As String * 80
End Type
Function BrowseCallbackProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal lParam As Long, ByVal lpData As String) As Long
Dim Path As String * MAX_PATH
If uMsg = BFFM_SELCHANGED Then
GetPathFromIDList lParam, Path
If Asc(Path) = 0 Then
Dim sfi As SHFILEINFO
SHGetFileInfo lParam, 0, sfi, Len(sfi), SHGFI_DISPLAYNAME Or SHGFI_PIDL
Path = sfi.szDisplayName
End If
SendMessage hwnd, BFFM_SETSTATUSTEXT, 0&, ByVal Path
ElseIf uMsg = BFFM_INITIALIZED Then
SendMessage hwnd, BFFM_SETSELECTION, True, ByVal lpData
SendMessage hwnd, WM_SETTEXT, 0, ByVal "Browse for Folder"
End If
End Function
Public Function BrowseFolder(StartFolder As String) As String
Dim bi As BROWSEINFO, pidl As Long, Path As String * MAX_PATH
CopyMemory bi.lpfn, AddressOf BrowseCallbackProc, 4
bi.ulFlags = BIF_RETURNONLYFSDIRS Or BIF_STATUSTEXT
bi.hwndOwner = Screen.ActiveForm.hwnd
bi.lParam = StrConv(Trim$(StartFolder), vbUnicode)
bi.lpszTitle = "Select a folder from the tree."
pidl = BrowseForFolder(bi)
If pidl Then
GetPathFromIDList pidl, Path
BrowseFolder = Left$(Path, InStr(Path, vbNullChar) - 1)
Else
BrowseFolder = StartFolder
End If
End Function
-----------------------------------------------------------
Now add a command button and a text box to your form and
paste the following code in your form.
-----------------------------------------------------------
Option Explicit
Private Sub Form_Load()
Text1 = App.Path
End Sub
Private Sub Command1_Click()
Text1 = BrowseFolder(Text1)
End Sub
-----------------------------------------------------------
Run the program and click the button to bring the dialog.