Public Type BROWSEINFO
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
'API declarations
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
Here let us use the above shell functions to open the browse directory dialog
Sub Show_BrowseDirectory_Dialog()
' BrowseForFolder
' SHBrowseForFolder API Function Example
Dim dirInfo As BROWSEINFO
Dim path As String
Dim r As Long, x As Long, pos As Integer
' Set Default Root folder = Desktop
dirInfo.pidlRoot = 0&
dirInfo.lpszTitle = "Browse directory!"
' Type of directory
dirInfo.ulFlags = &H1
' Show the Browse Dialog
x = SHBrowseForFolder(dirInfo)
' Parse the result
path = Space$(512)
r = SHGetPathFromIDList(ByVal x, ByVal path)
If r Then
pos = InStr(path, Chr$(0))
MsgBox "You have selected :=" & Left(path, pos - 1)
Else
MsgBox "Browse a Directory..."
Show_BrowseDirectory_Dialog
End If
End Sub
Very helpful, thank you!
ReplyDeletethnx a lot for this code
ReplyDelete