Results 1 to 4 of 4

Thread: How to get Common Dialog control to return a folder path

  1. #1

    Thread Starter
    Fanatic Member steve65's Avatar
    Join Date
    Jun 2000
    Posts
    610
    Are you asking yourself why doesn't he use the API's that have already been mentioned? Well I did and it works great, the only thing I didn't like about it was the you could not create a folder in the dialog if you wanted and I think the common dialog looks nicer with more options.

    So...
    Is there a way to hi-light a folder in the common dialog and click open and have the folder path passed like the API's do? I know how to set the filters and all just don't know how to return the folder path.

    Thanks!
    This space for rent...

  2. #2
    Guest
    Try this:

    Code:
    Option Explicit 
    
    Public 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 
    
    Public Const BIF_RETURNONLYFSDIRS = 1 
    Public Const MAX_PATH = 260 
    
    Public Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long) 
    Public Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long 
    Public Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long 
    Public Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long 
    
    Public Function BrowseForFolder(hwndOwner As Long, sPrompt As String) As String
         
        'declare variables to be used
         Dim iNull As Integer
         Dim lpIDList As Long
         Dim lResult As Long
         Dim sPath As String
         Dim udtBI As BrowseInfo
    
        'initialise variables
         With udtBI
            .hwndOwner = hwndOwner
            .lpszTitle = lstrcat(sPrompt, "")
            .ulFlags = BIF_RETURNONLYFSDIRS
         End With
    
        'Call the browse for folder API
         lpIDList = SHBrowseForFolder(udtBI)
         
        'get the resulting string path
         If lpIDList Then
            sPath = String$(MAX_PATH, 0)
            lResult = SHGetPathFromIDList(lpIDList, sPath)
            Call CoTaskMemFree(lpIDList)
            iNull = InStr(sPath, vbNullChar)
            If iNull Then sPath = Left$(sPath, iNull - 1)
         End If
    
        'If cancel was pressed, sPath = ""
         BrowseForFolder = sPath
    
    End Function
    
    
    Usage:
    
    Private Sub Command1_Click() 
    Dim strResFolder As String 
    strResFolder = BrowseForFolder(hWnd, "Please select a folder.") 
    If strResFolder = "" Then 
    Call MsgBox("The Cancel button was pressed.", vbExclamation) 
    Else 
    Call MsgBox("The folder " & strResFolder & " was selected.", vbExclamation) 
    End If 
    End Sub

  3. #3

    Thread Starter
    Fanatic Member steve65's Avatar
    Join Date
    Jun 2000
    Posts
    610
    Matthew,

    I thank you for the quick reply. This is what I was using already, but if the dialog was opened and user notice that he needed to create a dir, he would have to go to Windows Explorer, create the directory and then back to the Browse Dir dialog.

    I was trying to avoid this by using the common dialog.

    Is there no way to return a folder path using the common dialog box?

    Thanks,
    This space for rent...

  4. #4
    Guest
    Sorry about that. This code will get that path of a given filename.

    Code:
    Function GetFilePath(filename As String)
        Dim x As String
        Dim position As Integer
        Dim y As Integer
        Dim myPath As String
    
        x = filename
    
        For y = Len(x) To 1 Step -1
            If Mid(x, y, 1) = "\" Then
                position = y - 1
                Exit For
            End If
        Next y
    
    myPath = Left(x, position)
    
    GetFilePath = Format(myPath)
    
    End Function
    
    Usage:
    
    MsgBox GetFilePath("C:\Windows\Calc.exe")

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