PDA

View Full Version : Solved: Browsing for a folder



Ken Puls
08-24-2006, 02:36 PM
Hey guys,

I have an interesting issue that I've been banging my head against. I'm trying to open a dialog to browse for a folder. This is no biggie. The issue I have is that I want to supply a default path, and then let the user browse up/down the tree.

I started with the BrowseForFolder KB Entry (http://vbaexpress.com/kb/getarticle.php?kb_id=284) that I submitted. Here's the issue though:



Syntax oFolder = Shell.BrowseForFolder(Hwnd, sTitle, iOptions [,vRootFolder])Parameters

{snip}
vRootFolder Optional. Specifies a root folder for use in the dialog box. The user cannot browse higher in the tree than this folder. If this value is not specified, the root folder used in the dialog box is the desktop. {snip}

Anyone dealt with this before, or have an idea? I don't really want to start users at the Desktop level, as the default directory that I'm looking at is nested several levels down in the folder hierarchy.

I'm not after files here, just folders, so I don't know if GetOpenFilename will work for me either...

Bob Phillips
08-24-2006, 02:53 PM
Ken,

I think the problem is that the BrowseFolder facility can be passed an integer declaring the start folder, but these are the MS specialfolder constants, such as Desktop, Temp, Control Panel, etc. I don't think you can direct it to where you want.

Ken Puls
08-24-2006, 02:58 PM
Actually, if you pass it a string, it will open where you want. The problem is that this becomes the top level. So if you pass it say:

"C:\My Documents\Garbage"

That becomes the top level folder. You can't navigate back up to "C:\My Documents"

It seems to be a "by design" issue with BrowseForFolder. I'm just wondering if there is another way... one that doesn't involve me having to program the whole darn thing myself. The funtionality exists in GetOpenFilename, but I don't want Files, I want folders...

lucas
08-24-2006, 03:02 PM
Can't remember where I got it Ken but here is a userform solution for a Word doc. Maybe you can convert it to excel

mdmackillop
08-24-2006, 03:13 PM
Why not use Explorer?
http://vbaexpress.com/kb/getarticle.php?kb_id=801

Ken Puls
08-24-2006, 03:18 PM
Hi Malcolm,

I'm actually trying to collect a path so that my user can set it as a default path in future. I don't think shelling it out to Explorer is going to help me there. :)

Steve, I'm having a play with yours. It seems really slow to move up a level...

lucas
08-24-2006, 03:23 PM
Don't understand why Ken......not having that problem but would like to know what you decide about it. I usually don't go up a directory anyway but it is handy occationally.

mdmackillop
08-24-2006, 03:41 PM
Hi Ken,
There is a VB control DirListBox that I have used. There's a question and link here about incorporating it, but I've not followed it to the conclusion.
http://www.xtremevbtalk.com/printthread.php?t=71558

Ken Puls
08-24-2006, 03:59 PM
LOL! There's not much of a conclusion there...

mdmackillop
08-24-2006, 05:25 PM
Here's another userform solution I've cobbled together. You'll need to add some code to get the output.

lucas
08-24-2006, 06:24 PM
That works Malcolm and you can change the initialize to open to any directory:

Private Sub UserForm_Initialize()
TextBox1 = "F:\Temp\Temp" & "\"
DoList TextBox1
End Sub

Ken Puls
08-24-2006, 06:56 PM
Interesting...

Going to have a play with both, guys. :) One big thing... need to be able to change drives too.

The one thing I really liked about the browseforfolder was that you got the little plus signs beside the folder icons, so you knew if there was anything below it. It's a shame that we can't get those, but we do what we can, I guess.

Malcolm, at first glance, yours seemed not to show the hidden system/file folders. I'll need those as well. I'll have a play with it though, and see what I come up with.

Thanks, guys! :)

Bob Phillips
08-25-2006, 01:32 AM
I have been looking at the iOptions parameter, and this is the list I get



Const BIF_RETURNONLYFSDIRS = &H1 'Only return file system directories.
Const BIF_DONTGOBELOWDOMAIN = &H2 'Do not include network folders below
'the domain level n the dialog box's
'tree view control.
Const BIF_STATUSTEXT = &H4 'Include a status area in the dialog box.
Const BIF_RETURNFSANCESTORS = &H8 'Only return file system ancestors.
'An ancestor is a subfolder that is
'beneath the root folder in the namespace hierarchy.
Const BIF_EDITBOX = &H10 '(SHELL32.DLL Version 4.71). Include an
'edit control in the browse dialog box
'that allows the user to type the name of an item.
Const BIF_VALIDATE = &H20 '(SHELL32.DLL Version 4.71). If the user
'types an invalid name into the edit box,
'the browse dialog will call the application's
'BrowseCallbackProc with the BFFM_VALIDATEFAILED message.
Const BIF_USENEWUI = &H40 '(SHELL32.DLL Version 5.0).
'Use the new user interface, including an edit box.
Const BIF_NEWDIALOGSTYLE = &H50 '(SHELL32.DLL Version 5.0).
'Use the new user interface.
Const BIF_BROWSEINCLUDEURLS = &H80 '(SHELL32.DLL Version 5.0).
'The browse dialog box can display URLs.
'The BIF_USENEWUI and BIF_BROWSEINCLUDEFILES flags must also be set.
Const BIF_BROWSEFORCOMPUTER = &H1000 'Only return computers.
Const BIF_BROWSEFORPRINTER = &H2000 'Only return network printers.
Const BIF_BROWSEINCLUDEFILES = &H4000 '(SHELL32.DLL Version 4.71).
'The browse dialog will display files as well as folders.
Const BIF_SHAREABLE = &H8000 '(SHELL32.DLL Version 5.0).
'The browse dialog box can display shareable
'resources on remote systems.
'The BIF_USENEWUI flag must also be set.


doesn't look like there is an option.

uksrogers
08-25-2006, 03:50 AM
Have a feeling i could end up stating something really obvious here, but have you tried the Application.FileDialog(msoFileFolderPicker) option? There is an initialfilename property that allows you to specify a starting folder, but it still allows you to navigate wherever you like.

uksrogers

Ken Puls
08-25-2006, 08:39 AM
ROFL!

uksrogers, that is EXACTLY what I am looking for! I tend to miss the obvious somtimes. ;)

Here it is:
Sub test()
Dim sFile As Variant
Dim lngcount
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = "C:\My Documents\"
.Show
For lngcount = 1 To .SelectedItems.Count
sFile = .SelectedItems(lngcount)
Next lngcount
End With
Debug.Print sFile
End Sub

Thank you!

Ken Puls
08-25-2006, 08:41 AM
Oh, and btw guys... I played with each of your files last night, and also tried reinventing the wheel with a Treeview solution. I learned a bit from that, I'll tell you!

All in all, though, this is Exactly what I need. Thanks again for you interest and help on it. :hi:

Ken Puls
08-25-2006, 02:38 PM
FYI,

The FileDialogFilter was a new object added in Office 2002 (XP). Prior version will not support this object.

Ma7
12-08-2013, 05:24 AM
Unfortunately, I am facing the same problem and this solution does not work with Outlook, I get an error although the office library is included in the reference. I read that FileDialog does not work in outlook. Any ideas?

ROFL!

uksrogers, that is EXACTLY what I am looking for! I tend to miss the obvious somtimes. ;)

Here it is:
Sub test()
Dim sFile As Variant
Dim lngcount
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = "C:\My Documents\"
.Show
For lngcount = 1 To .SelectedItems.Count
sFile = .SelectedItems(lngcount)
Next lngcount
End With
Debug.Print sFile
End Sub

Thank you!