Option Explicit
Public Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
Public Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Public Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Public Type BROWSEINFO
hOwner As Long 'handle to the window calling this
pidlRoot As Long 'ID List point to the top most folder, set this to 0
pszDisplayName As Long 'Buffer used to hold the display name of the folder selected by the user
lpszTitle As String 'the caption of the browse dialog
uFlags As Long 'the flags to determine what to browse for
lpfn As Long 'the address of the callback function, set to Null
lParam As Long 'a value passed to the callback function
iImage As Long 'a buffer to hold the index to the image of selected folder
End Type
Public Const BIF_RETURNONLYFSDIRS = &H1 ' - file system folders only
Public Const BIF_BROWSEFORCOMPUTER = &H1000 ' - allows you to browse for a computer
Public Const BIF_BROWSEFORPRINTER = &H2000 '- browse Printers Folder
Public Sub Show_Browse()
Dim bi As BROWSEINFO 'structure we will fill later
Dim pIdList As Long 'the return value (pointer to ID List)
Dim strFolder As String 'extracted value from pIdList
Dim lHWnd As Long
strFolder = String$(255, Chr$(0)) 'create buffer large enough to hold folder path
With bi 'populate BROWSEINFO structure
.hOwner = lHWnd ' hwnd of the calling form
.uFlags = BIF_RETURNONLYFSDIRS ' browse for sysfolders
.pidlRoot = 0 '
.lpszTitle = "Select a folder" & Chr$(0) 'zero-terminated string
End With
pIdList = SHBrowseForFolder(bi) 'initial fucntion call to get ID List pointer
'extract file path
If SHGetPathFromIDList(ByVal pIdList, ByVal strFolder) Then
MsgBox Left$(strFolder, InStr(strFolder, Chr$(0)) - 1)
End If
End Sub