Results 1 to 5 of 5

Thread: browsing for folder

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 1999
    Posts
    38

    Post

    i want to allow a user to browse for a directory, not a file. i think the common dialog must return a file, how would i go about selecting and returning a directory?

  2. #2
    Junior Member
    Join Date
    Oct 1999
    Location
    Colorado Springs
    Posts
    17

    Post

    put a directory list box on your form and read the results on a label or something (I think this will work...I picked the code out of a bigger block).

    Private Sub Dirdirectory_Change()
    Dim spath
    spath = Dirdirectory.Path

    lbldirname.Caption = Dirdirectory.Path 'update drive label

  3. #3

  4. #4
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    Sure thing:

    Code:
    Private Type BROWSEINFO 'bi
        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
    
    Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
    Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
    Private Const BIF_RETURNONLYFSDIRS = &H1
    
    Public Function ShowFolderDialog(pHwnd As Long) As String
        Dim biStruct As BROWSEINFO
        Dim lPidl As Long
        Dim lRet As Long
        Dim intPos As Integer
        Dim strPath As String
        
        biStruct.hOwner = pHwnd
        biStruct.pidlRoot = 0
        biStruct.lpszTitle = "Select Folder"
        biStruct.ulFlags = BIF_RETURNONLYFSDIRS
        lPidl = SHBrowseForFolder(biStruct)
        strPath = Space(512)
        lRet = SHGetPathFromIDList(ByVal lPidl, ByVal strPath)
        If lRet Then
            intPos = InStr(strPath, vbNullChar)
            ShowFolderDialog = Left(strPath, intPos - 1)
        End If
    End Function

    Usage: Result = ShowFolderDialog(OwnedWindowHwnd As Long)

    Example: MsgBox ShowFolderDialog(Me.hWnd)

    Regards,

    ------------------

    Serge

    Software Developer
    [email protected]
    [email protected]



  5. #5
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    If you're already using the CommonDialog elsewhere in your application an don't want to add the extra code for the Folder Browser, a quick modification to the CommonDialog Properties can yield satisfactory results..
    Code:
    Private Sub Command1_Click()
        Dim sDir As String
        With CommonDialog1
            .Flags = cdlOFNNoValidate
            .FileName = "*"
            .ShowOpen
            sDir = Left(.FileName, Len(.FileName) - Len(.FileTitle) - 1)
        End With
        Debug.Print sDir 'Display Select Folder Path
    End Sub

    ------------------
    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width