Here's come code which I located years ago on the internet.
- Malc
www.dragondrop.com
Option Explicit
Private Const MAX_PATH = 260
Private Type SHITEMID ' mkid
cb As Long ' Size of the ID (including cb itself)
abID() As Byte ' The item ID (variable length)
End Type
Private Type SHFILEINFO ' shfi
hIcon As Long
iIcon As Long
dwAttributes As Long
szDisplayName As String * MAX_PATH
szTypeName As String * 80
End Type
Private Type ITEMIDLIST ' idl
mkid As SHITEMID
End Type
Private Declare Function SHGetPathFromIDList Lib "Shell32.dll" Alias
"SHGetPathFromIDListA" _
(ByVal pidl As Long, ByVal pszPath As
String) As Long
' Frees memory allocated by SHBrowseForFolder()
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal pv As Long)
Private Declare Function SHBrowseForFolder Lib "Shell32.dll" Alias
"SHBrowseForFolderA" _
(lpBrowseInfo As BROWSEINFO) As Long '
Private 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
'
'
Public Function SelectFolder(sCaption As String) As String
Dim BI As BROWSEINFO
Dim nFolder As Long
Dim IDL As ITEMIDLIST
Dim pidl As Long
Dim sPath As String
Dim SHFI As SHFILEINFO
Dim sSelectedFolder As String
On Error Resume Next
sSelectedFolder = ""
With BI
'// The dialog'//s owner window...
'.hOwner = Me.hWnd
'// Initialize the buffer that rtns the display name of the selected
folder
.pszDisplayName = String$(MAX_PATH, 0)
'// Set the dialog'//s banner text
.lpszTitle = sCaption
'// Set the type of folders to display & return
'// -play with these option constants to see what can be returned
'.ulFlags = GetReturnType()
End With
'// if you stop code execution between here and the
'// end of this sub, you will be wasting memory.
'// you need to call CoTaskMemFree pIdl to free the
'// memory used by SHBrowseForFolder
'// Show the Browse dialog
pidl = SHBrowseForFolder(BI)
'// If the dialog was cancelled...
If pidl > 0 Then
'// Fill sPath w/ the selected path from the id list
'// (will rtn False if the id list can'//t be converted)
sPath = String$(MAX_PATH, 0)
SHGetPathFromIDList ByVal pidl, ByVal sPath
'// Display the path and the name of the selected folder
sSelectedFolder = Left$(sPath, InStr(sPath, vbNullChar) - 1)
'// Frees the memory SHBrowseForFolder()
'// allocated for the pointer to the item id list
End If
CoTaskMemFree pidl
SelectFolder = sSelectedFolder
End Function