Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Browse for folder (Office All)

    Element K Tips just had a great tip for a really simple Browse For Folder. It was for VB, so I translated it for VBA. If you use this code, don't forget to use the Tools | References menu to add a reference to Microsoft Shell Controls and Automation.
    <pre>Option Explicit
    Declare Function GetActiveWindow Lib "user32" () As Long
    ' Set a reference to Microsoft Shell Controls and Automation for Shell32:
    Private shlShell As Shell32.Shell
    Private shlFolder As Shell32.Folder
    Private Const BIF_RETURNONLYFSDIRS = &H1

    Public Sub Browse()
    If shlShell Is Nothing Then
    Set shlShell = New Shell32.Shell
    End If
    Set shlFolder = shlShell.BrowseForFolder _
    (GetActiveWindow, "Select a Directory", BIF_RETURNONLYFSDIRS)
    If Not shlFolder Is Nothing Then
    MsgBox shlFolder.Title
    End If
    End Sub</pre>

    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Browse for folder (Office All)

    And mix and match for all sorts of fun!

    Public Const BIF_RETURNONLYFSDIRS = &H1
    Public Const BIF_DONTGOBELOWDOMAIN = &H2
    Public Const BIF_STATUSTEXT = &H4
    Public Const BIF_RETURNFSANCESTORS = &H8
    Public Const BIF_BROWSEFORCOMPUTER = &H1000
    Public Const BIF_BROWSEFORPRINTER = &H2000
    Public Const BIF_EDITBOX = &H10
    Public Const BIF_USENEWUI = &H40

    e.g.: (GetActiveWindow, "Select a Directory", BIF_RETURNONLYFSDIRS+BIF_USENEWUI+BIF_RETURNFSANCE STORS )
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  4. #3
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Browse for folder (Office All)

    Sammy,

    It only seems to return the name of folder selected rather than the full path to that folder. Brwsing to "C:Program FilesMicrosoft OfficeOffice10Samples" returns only Samples.

    An amendment to
    <pre> MsgBox shlFolder.ParentFolder.ParseName(shlFolder.Title). Path</pre>

    should rectify this.

    The following version does not require any reference to be set.<pre>Sub FolderBrowser()
    Dim oFolder
    Set oFolder = CreateObject("Shell.Application"). _
    BrowseForFolder(0, "Select a Folder : ", &H1)
    If Not oFolder Is Nothing Then
    MsgBox oFolder.ParentFolder.ParseName(oFolder.Title).Path
    Set oFolder = Nothing
    End If
    End Sub</pre>

    A further adaption could be made to both versions to allow a preset path when the dialog opens. Just pass an additional argument after the flags value,

    e.g .<pre>Set shlFolder = shlShell.BrowseForFolder _
    (GetActiveWindow, "Select a Directory", BIF_RETURNONLYFSDIRS, "C:Program Files")</pre>

    would open the browser with "C:Program Files" pre-selected.

    Andrew

  5. #4
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Browse for folder (Office All)

    Nicely done, Andrew.

    Now folks, a little quiz:

    Why use GetActiveWindow instead of zero as the first BrowseForFolder parameter?

    As in...
    .BrowseForFolder(0, "Select a Folder : ", &H1)
    vs.
    .BrowseForFolder(GetActiveWindow, "Select a Folder : ", &H1)

    Try it out and you'll spot the difference right away.

    Months ago I spent hours trying to figure out how to center the Browse For Folder dialog
    on the screen...

    ...pass it the active window handle. That's how.

    Also, did you know you can use this object/dialog to Browse for files and printers as well?
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  6. #5
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Browse for folder (Office All)

    Kevin,

    I tested your GetActiveWindow theory on Windows XP and and Windows ME , and in neither case did it make a difference. Is there some other little tune-up needed ?

    As for Browsing for Printers, Files etc, I have found in the past some unreliable results using this approach, but these are resolved using the SHBrowseForFolder API function.

    Also there may be issues with NT. Have you tried it with NT ?

    See This thread

    Andrew

  7. #6
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Browse for folder (Office All)

    Kevin,

    Having tried again, the dialog is centering with GetActiveWindow on the XP box (it was not earlier), but not on the ME system.

    Andrew

  8. #7
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Browse for folder (Office All)

    Ah!

    Ok, I see the problem. The Browse dialog displays relative to the upper left corner of the activewindow. If the
    window handle passed into the Shell version is zero, the dialog is positioned relative to the desktop. This is
    the same as the API verions, AFAICT.

    I discovered that if my underlying form window is centered AND I pass Browse the handle of that centered window
    I get Browse "centered" -- not exactly, but good enough. --Not quite what I thought when I posted. Sorry.

    Try turning on the editbox and set the root in order to get the printers to work:

    Set oFolder = CreateObject("Shell.Application").BrowseForFolder( GetActiveWindow, strTitle, _
    BIF_BROWSEFORPRINTER + BIF_EDITBOX, &H4)
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  9. #8
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Browse for folder (Office All)

    Again, I believe it's setting the Browse dialog position relative to the activewindow position.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  10. #9
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Browse for folder (Office All)

    The next challenge is to get the Shell version to handle the SpecialFolders.
    If you allow those to display in the dialog and select "My Computer", you'll get
    ::{20D04FE0-3AEA-1069-A2D8-08002B30309D}. Some sort of Class ID??
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  11. #10
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Browse for folder (Office All)

    Upon further testing with

    Set oFolder = CreateObject("Shell.Application").BrowseForFolder( 0, strTitle, _
    BIF_BROWSEFORPRINTER + BIF_EDITBOX, &H4)

    ...I think you're right. It does display fine (except the "clicked" icon is busted)

    but "object doesn't support this action."
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  12. #11
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Browse for folder (Office All)

    Most probably - but some research still needed I think.

    I tried your suggestion of turning on the Edit box and it worked as suggested.

    However

    Set oFolder = CreateObject("Shell.Application").BrowseForFolder( GetActiveWindow, strTitle, BIF_BROWSEFORPRINTER , &H4)

    worked just as well because the key was the inclusion of the value &H4, which dictates which folder is pre-selected. &H4 must represent a virtual folder for available printers.

    Andrew

  13. #12
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Browse for folder (Office All)

    Agreed.

    Here:

    ssfPRINTERS. Virtual folder containing installed printers. (value = 4)
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  14. #13
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Browse for folder (Office All)

    Andrew,

    >Set oFolder = CreateObject("Shell.Application").BrowseForFolder( GetActiveWindow, strTitle, BIF_BROWSEFORPRINTER , &H4)

    Without BIF_EDITBOX, the Ok button on the dialog is never enabled. This, on Win2k SP3.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  15. #14
    Lounger
    Join Date
    Jun 2002
    Location
    Toronto, Ontario
    Posts
    45
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Browse for folder (Office All)

    Just wondered if you could post the original "for VB" version.

    Also, is this effective across version of Windows, NT NT2000 98 etc.?

    Thanks
    JOhn

  16. #15
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Browse for folder (Office All)

    As published in the Element K Journal:

    Private shlShell As Shell32.Shell
    Private shlFolder As Shell32.Folder
    Private Const BIF_RETURNONLYFSDIRS = &H1

    Private Sub Command1_Click()
    If shlShell Is Nothing Then
    Set shlShell = New Shell32.Shell
    End If
    Set shlFolder = shlShell.BrowseForFolder(Me.hWnd, "Select a Directory", BIF_RETURNONLYFSDIRS)
    If Not shlFolder Is Nothing Then
    MsgBox shlFolder.Title
    End If
    End Sub
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

Page 1 of 2 12 LastLast

Posting Permissions

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