VB Notebook Main Page VB Notebook Files VB Notebook Function Library VB Notebook Articles VB Notebook Links VB Notebook

VBNotebook Home | Functions | Folder Dialog

This function opens a dialog control that displays the directories/folders available to the user and allows them to make a selection. The hWndOwner routine is the window handle of the calling program's active window, but if the default is used, the desktop window handle is used. The sTitle parameter optionally sets the caption area of the dialog to the informational text you wish to display.

The following code is needed in the General|Declarations area of the module.
__________________________________________________________________________________________

'
' Types
'
    Private Type BrowseInfo
       
hWndOwner As Long
       
pIDLRoot As Long
       
pszDisplayName As Long
       
lpszTitle As Long
       
ulFlags As Long
       
lpfnCallback As Long
       
lParam As Long
       
iImage As Long
    End Type
'
' GetFolder Constants
'
    Private Const BIF_RETURNONLYFSDIRS = 1
    Private Const
BIF_DONTGOBELOWDOMAIN = 2
'
' API Calls
'
   
Private Declare Function GetDesktopWindow Lib "user32" () As Long

    Private Declare Function
lstrcat Lib "kernel32" Alias "lstrcatA" _
                            
(ByVal lpString1 As String, _
                              ByVal
lpString2 As String) 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

__________________________________________________________________________________________

Now, the routine itself....
__________________________________________________________________________________________

Public Function GetFolder(Optional ByVal hWndOwner As Long = 0, _
                          Optional ByVal sTitle As String = "") As String

    Dim lpIDList As Long
    Dim sBuffer As String
    Dim tBrowseInfo As BrowseInfo

    If hWndOwner = 0 Then
        hWndOwner = GetDesktopWindow()
    End If
    With tBrowseInfo
        .hWndOwner = hWndOwner
        .lpszTitle = lstrcat(szTitle, "")
        .ulFlags = BIF_RETURNONLYFSDIRS + BIF_DONTGOBELOWDOMAIN
    End With
    lpIDList = SHBrowseForFolder(tBrowseInfo)
    If lpIDList Then
        sBuffer = Space$(MAX_PATH)
        SHGetPathFromIDList lpIDList, sBuffer
        sBuffer = Left$(sBuffer, InStr(sBuffer, vbNullChar) - 1)
    Else
        sBuffer = vbNullChar
    End If
    GetFolder = sBuffer
End Function

__________________________________________________________________________________________

Copyright 2000-2005, J. Frank Carr