Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 177
  • Last Modified:

Selecting a directory.

Does anybody know of an easy way to select just a directory using the common dialog control? Or perhaps another way that doesn't involve writing a form with a directory selector on it.
0
egunn
Asked:
egunn
  • 3
  • 2
1 Solution
 
EDDYKTCommented:
Option Explicit
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 Type BROWSEINFO
    HwndOwner 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 Sub Command1_Click()
    Dim Info As BROWSEINFO
    Dim PID As Long, Path As String, Tmp As String
    Dim localpath As String
     
    Info.HwndOwner = Me.hWnd
    Info.PidlRoot = &H0
    Info.LpszTitle = "Select File Path:"
    Info.UlFlags = &H1
    PID = SHBrowseForFolder(Info)
    Path = Space$(512)
    If (SHGetPathFromIDList(PID, Path)) Then localpath = Left$(Path, InStr(Path, Chr$(0)) - 1)
End Sub

If cancel is pressed, localpath is ""


0
 
mcriderCommented:
Here you go!


Dim iVal As Long
Dim SelectedPath As String

On Error Resume Next
With CommonDialog1
    .filename = "PATH"
    .Flags = cdlOFNExplorer
    .CancelError = True
    .Action = 1
    If Err = 0 Then
        For iVal = Len(.filename) To 1 Step -1
            If Mid$(.filename, iVal, 1) = "\" Then
                SelectedPath = Left$(.filename, iVal - 1)
                Exit For
            End If
        Next iVal
    End If
End With
MsgBox "You Selected:  " + SelectedPath


Cheers!®©
0
 
egunnAuthor Commented:
Sorry EDDYKT's answer is more elegant.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
egunnAuthor Commented:
Brilliant , I don't suppose you know how to do and FTP directory as well do you. More points in it.
0
 
EDDYKTCommented:
For ftp directory, you can either use wininet.dll or msinet.ocx to build your own directory dialog
0
 
egunnAuthor Commented:
Thanks, I thought about that but it just seems like a lot of hassel to go to. I've posted another question asking this hopefully somebody's already done it and will let me have the code.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now