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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting a Folder (not a file)

Opening Files

Selecting a Folder (not a file)

by  BigTeeJay  Posted    (Edited  )
I remember reading somewhere that this isnt possible, which I found hard to believe (especially since I am sure everyone has seen it done already at one time or another). The below code was supposed to be for VB (but works just as well for VBA)

The below code was found at...
http://www.vb-helper.com/howtoint.htm

====== Begin Code Block ========[tt]
[color blue]Option Explicit[/color]

[color blue]Private Type[/color] BrowseInfo
hWndOwner [color blue]As Long[/color]
pidlRoot [color blue]As Long[/color]
sDisplayName [color blue]As String[/color]
sTitle [color blue]As String[/color]
ulFlags [color blue]As Long[/color]
lpfn [color blue]As Long[/color]
lParam [color blue]As Long[/color]
iImage [color blue]As Long
End Type

Private Declare Function[/color] SHBrowseForFolder [color blue]Lib[/color] "Shell32.dll" (bBrowse [color blue]As[/color] BrowseInfo) [color blue]As Long
Private Declare Function[/color] SHGetPathFromIDList [color blue]Lib[/color] "Shell32.dll" ([color blue]ByVal[/color] lItem [color blue]As Long, ByVal[/color] sDir [color blue]As String[/color]) [color blue]As Long[/color]
[color green]
' Let the user browse for a directory. Return the
' selected directory. Return an empty string if
' the user cancels.[/color]
[color blue]Private Function[/color] BrowseForDirectory() [color blue]As String[/color]
[color blue]Dim[/color] browse_info [color blue]As BrowseInfo[/color]
[color blue]Dim[/color] item [color blue]As Long[/color]
[color blue]Dim[/color] dir_name [color blue]As String[/color]

[color green]'modified for MS Access/VBA[/color]
[color blue]With[/color] browse_info
.hWndOwner = Application.hWndAccessApp
.pidlRoot = 0
.sDisplayName = Space$(260)
.sTitle = "Select Directory"
.ulFlags = 1 [color green]' Return directory name.[/color]
.lpfn = 0
.lParam = 0
.iImage = 0
[color blue]End With[/color]

item = SHBrowseForFolder(browse_info)
[color blue]If[/color] item [color blue]Then[/color]
dir_name = Space$(260)
[color blue]If[/color] SHGetPathFromIDList(item, dir_name) [color blue]Then[/color]
BrowseForDirectory = Left(dir_name, _
InStr(dir_name, Chr$(0)) - 1)
[color blue]Else[/color]
BrowseForDirectory = ""
[color blue]End If
End If
End Function

Private Sub[/color] Command1_Click()
MsgBox BrowseForDirectory
[color blue]End Sub[/color] [/tt]
======= End Code Block ========

You can download a sample project which utilizes this from their website. It's not easy to find, so here is the link...
http://www.vb-helper.com/HowTo/browsdir.zip

Regards,
BigTeeJay
spam__me (at) excite (dot) com
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top