Visual Basic Programming Code Examples Visual Basic > API and Miscellaneous Code Examples Show the 'Browse Folder' Common Dialog via API calls Show the 'Browse Folder' Common Dialog via API calls To show the browse for folder common dialog use the following routine: Private Type BrowseInfo hWndOwner As Long pIDLRoot As Long pszDisplayName As Long lpszTitle As String ulFlags As Long lpfnCallback As Long lParam As Long iImage As Long End Type Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long) Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long Private Declare Function GetActiveWindow Lib "user32" () As Long 'Purpose : Allows the user to select a folder 'Inputs : sCaption The caption text on the dialog ' [sDefault] The default path to return if the user presses cancel 'Outputs : Returns the select path Function BrowseForFolder(Optional sCaption As String = "Select a folder", Optional sDefault As String) As String Const BIF_RETURNONLYFSDIRS = 1 Const MAX_PATH = 260 Dim lPos As Integer, lpIDList As Long, lResult As Long Dim sPath As String, tBrowse As BrowseInfo With tBrowse 'Set the owner window .hWndOwner = GetActiveWindow 'Me.hWnd in VB .lpszTitle = sCaption .ulFlags = BIF_RETURNONLYFSDIRS 'Return only if the user selected a directory End With 'Show the dialog lpIDList = SHBrowseForFolder(tBrowse) If lpIDList Then sPath = String$(MAX_PATH, 0) 'Get the path from the IDList SHGetPathFromIDList lpIDList, sPath CoTaskMemFree lpIDList lPos = InStr(sPath, vbNullChar) If lPos Then BrowseForFolder = Left$(sPath, lPos - 1) If Right$(BrowseForFolder, 1) <> "\" Then BrowseForFolder = BrowseForFolder & "\" End If End If Else 'User cancelled, return default path BrowseForFolder = sDefault End If End Function