Consulting

Results 1 to 18 of 18

Thread: Solved: Browsing for a folder

  1. #1
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location

    Solved: Browsing for a folder

    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 that I submitted. Here's the issue though:

    Quote Originally Posted by Microsoft
    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...
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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.

  3. #3
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    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...
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    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
    Last edited by lucas; 08-24-2006 at 03:20 PM. Reason: Personal info removed
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    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...
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  7. #7
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    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.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    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
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    LOL! There's not much of a conclusion there...
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Here's another userform solution I've cobbled together. You'll need to add some code to get the output.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  11. #11
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    That works Malcolm and you can change the initialize to open to any directory:
    [VBA]
    Private Sub UserForm_Initialize()
    TextBox1 = "F:\Temp\Temp" & "\"
    DoList TextBox1
    End Sub
    [/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  12. #12
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    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!
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  13. #13
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I have been looking at the iOptions parameter, and this is the list I get

    [vba]

    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.
    [/vba]

    doesn't look like there is an option.

  14. #14
    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

  15. #15
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    ROFL!

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

    Here it is:
    [vba]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[/vba]

    Thank you!
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  16. #16
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    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.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  17. #17
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    FYI,

    The FileDialogFilter was a new object added in Office 2002 (XP). Prior version will not support this object.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  18. #18
    VBAX Newbie
    Joined
    Dec 2013
    Posts
    1
    Location
    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?
    Quote Originally Posted by Ken Puls View Post
    ROFL!

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

    Here it is:
    [vba]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[/vba]

    Thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •