VB Code:
  1. Option Explicit
  2.  
  3. Public Type BrowseInfo
  4.      hwndOwner As Long
  5.      pIDLRoot As Long
  6.      pszDisplayName As Long
  7.      lpszTitle As Long
  8.      ulFlags As Long
  9.      lpfnCallback As Long
  10.      lParam As Long
  11.      iImage As Long
  12. End Type
  13. Public Const BIF_RETURNONLYFSDIRS = 1
  14. Public Const MAX_PATH = 260
  15.  
  16. Public Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
  17. Public Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
  18. Public Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
  19. Public Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
  20.  
  21. Public Function BrowseForFolder(hwndOwner As Long, sPrompt As String) As String
  22.      
  23.     'declare variables to be used
  24.      Dim iNull As Integer
  25.      Dim lpIDList As Long
  26.      Dim lResult As Long
  27.      Dim sPath As String
  28.      Dim udtBI As BrowseInfo
  29.  
  30.     'initialise variables
  31.      With udtBI
  32.         .hwndOwner = hwndOwner
  33.         .lpszTitle = lstrcat(sPrompt, "")
  34.         .ulFlags = BIF_RETURNONLYFSDIRS
  35.      End With
  36.  
  37.     'Call the browse for folder API
  38.      lpIDList = SHBrowseForFolder(udtBI)
  39.      
  40.     'get the resulting string path
  41.      If lpIDList Then
  42.         sPath = String$(MAX_PATH, 0)
  43.         lResult = SHGetPathFromIDList(lpIDList, sPath)
  44.         Call CoTaskMemFree(lpIDList)
  45.         iNull = InStr(sPath, vbNullChar)
  46.         If iNull Then sPath = Left$(sPath, iNull - 1)
  47.      End If
  48.  
  49.     'If cancel was pressed, sPath = ""
  50.      BrowseForFolder = sPath
  51.  
  52. End Function
  53.  
  54.  
  55.  
  56. 'usage :
  57. Private Sub Form_Click()
  58.     MsgBox BrowseForFolder(Me.hWnd, "select folder")
  59. End Sub