Home All Groups Group Topic Archive Search About

Get directory selection

Author
11 Nov 2005 6:44 PM
David
I use a GetOpenFileName function and the common widows dialog to return the
path and name of a selected file.  However, I now want to be able to
retrieve just a folder path\name, even if there are no files in that folder.
Can anyone point me where I can get this function?  Thanks.

David

Author
11 Nov 2005 6:52 PM
Matt Williamson
David-

You want a Browse for Folders dailog.

Use this API

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

Do a search and you'll find plenty of code examples.

HTH

Matt

Show quoteHide quote
"David" <dlch***@lifetimeinc.com> wrote in message
news:OUC7w$u5FHA.472@TK2MSFTNGP15.phx.gbl...
>I use a GetOpenFileName function and the common widows dialog to return the
>path and name of a selected file.  However, I now want to be able to
>retrieve just a folder path\name, even if there are no files in that
>folder. Can anyone point me where I can get this function?  Thanks.
>
> David
>
Author
11 Nov 2005 7:02 PM
mike williams
"David" <dlch***@lifetimeinc.com> wrote in message
news:OUC7w$u5FHA.472@TK2MSFTNGP15.phx.gbl...

> I want to be able to retrieve just a folder path\name, even if
> there are no files in that folder.

Add another Form to your project (Form2 in the example) and paste in the
following code:

Option Explicit
Private Type BrowseInfo
  hWndOwner As Long
  pidlRoot As Long
  sDisplayName As String
  sTitle As String
  ulFlags As Long
  lpfn As Long
  lParam As Long
  iImage As Long
End Type
Private Declare Function SHBrowseForFolder Lib "shell32.dll" _
(bBrowse As BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _
(ByVal lItem As Long, ByVal sDir As String) As Long

Private Function Browse_Folder() As String
Dim browse_info As BrowseInfo
Dim lItem As Long
Dim sDirName As String
browse_info.hWndOwner = Me.hWnd
browse_info.pidlRoot = 0
browse_info.sDisplayName = Space$(260)
browse_info.sTitle = "Select Folder"
browse_info.ulFlags = 1 ' Only show folders.
'browse_info.ulFlags = 1 Or &H4000 ' Show folders and files.
browse_info.lpfn = 0
browse_info.lParam = 0
browse_info.iImage = 0
lItem = SHBrowseForFolder(browse_info)
If lItem Then
  sDirName = Space$(260)
  If SHGetPathFromIDList(lItem, sDirName) Then
    Browse_Folder = Left(sDirName, InStr(sDirName, Chr$(0)) - 1)
  Else
    Browse_Folder = ""
  End If
End If
End Function

Private Sub Form_Activate()
Dim sfolder As String
sfolder = Browse_Folder
If sfolder = "" Then
  Form1.Tag = "User cancelled"
Else
  Form1.Tag = sfolder
End If
Unload Me
End Sub

Private Sub Form_Load()
Me.Top = -Me.Height
End Sub

Then use the following code in a Command Button on your main Form. The name
/ path of the selected folder is stored in the Tag property of Form2.

Private Sub Command1_Click()
' browse for folder
Form2.Show vbModal
Caption = Tag
End Sub

Mike