Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do i use a dialog to find just a path (not a file) ?

Status
Not open for further replies.

IanMcA

Computer
Joined
Sep 25, 2003
Messages
1
Location
GB
I'd like to use a dialog box to allow the user to find the path to a set of folders but what i've seen so far seems to only work when you select a file (otherwise the 'open' or 'save' button remains greyed out.

Is there any way to use a dialog to allow the user to select just the path without having to select a file?

In this instance i'm using Excel 97 SR-2

Thanks in Advance

Ian
 
Code:
'32-bit API declarations
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
  Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long

Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long

Public Type BROWSEINFO
    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

public sub DoSomething()
    'Prompt user to select folder
    Dim lFolder As String
    lFolder = GetDirectory("Select the folder")
    If (lFolder = "") Then
        Exit Sub
    End If
    MsgBox "You picked: " & lFolder
end sub

Public Function GetDirectory(Optional Msg) As String
    Dim bInfo As BROWSEINFO
    Dim path As String
    Dim r As Long, x As Long, pos As Long
 
'   Root folder = Desktop
    bInfo.pidlRoot = 0&

'   Title in the dialog
    If IsMissing(Msg) Then
        bInfo.lpszTitle = "Select a folder."
    Else
        bInfo.lpszTitle = Msg
    End If
    
'   Type of directory to return
    bInfo.ulFlags = &H1

'   Display the dialog
    x = SHBrowseForFolder(bInfo)
    
'   Parse the result
    path = Space$(512)
    r = SHGetPathFromIDList(ByVal x, ByVal path)
    If r Then
        pos = InStr(path, Chr$(0))
        GetDirectory = Left(path, pos - 1)
    Else
        GetDirectory = ""
    End If
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top