# VB6 - Selecting folders in CommonDialog



## asbo (Sep 21, 2001)

heya,

I'm writing a piece of software that requires the user to select a directory -- NOT a file -- for use in a TextBox. I know how to handle the CmnDlg's output and such, but how can I make it so that the user can select a folder, as appose to a file? I've the "cdlOFNExplorer" Flag to no sucess. Can anyone help me out with this? I've pretty much scoured MSDN, and can't find anything related to this.

Thanks,
asbo


----------



## Mosaic1 (Aug 17, 2001)

Select a Directory as output to a text box? Not sure what you are asking. Do you want the name of the folder to show in the text box? Or the contents of the folder?
I think you may have to use the DirList box and not the FILE save dialog.


----------



## asbo (Sep 21, 2001)

Mo,

I cleaned up a little of the wording above; see if that helps. A visual rep of what I'm talking about is kinda like when you've got an explorer window open (say to C:\) and you click the Folders option on the toolbar, only that that, stick in a dialog and put OK an Cancel beneath it.

That nay better?

Thanks,
asbo


----------



## TimCottee (Oct 10, 2001)

This is what you are looking for:


```
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
Const BIF_RETURNONLYFSDIRS = 1
Const MAX_PATH = 260
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem 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
Private Sub Form_Load()
    'KPD-Team 1998
    'URL: [url]http://www.allapi.net/[/url]
    '[email protected]
    Dim iNull As Integer, lpIDList As Long, lResult As Long
    Dim sPath As String, udtBI As BrowseInfo

    With udtBI
        'Set the owner window
        .hWndOwner = Me.hWnd
        'lstrcat appends the two strings and returns the memory address
        .lpszTitle = lstrcat("C:\", "")
        'Return only if the user selected a directory
        .ulFlags = BIF_RETURNONLYFSDIRS
    End With

    'Show the 'Browse for folder' dialog
    lpIDList = SHBrowseForFolder(udtBI)
    If lpIDList Then
        sPath = String$(MAX_PATH, 0)
        'Get the path from the IDList
        SHGetPathFromIDList lpIDList, sPath
        'free the block of memory
        CoTaskMemFree lpIDList
        iNull = InStr(sPath, vbNullChar)
        If iNull Then
            sPath = Left$(sPath, iNull - 1)
        End If
    End If

    MsgBox sPath
End Sub
```


----------



## asbo (Sep 21, 2001)

Tim,

I was waiting for you to come!  Thanks again. Any suggestions on possibly turning this into its own Sub, so I can just call on it when I need it?

Thanks,
asbo

[EDIT: Nevermind the Sub part; I actually figured something out for my self once  . Thanks again!!]


----------

