Results 1 to 9 of 9

ShellApp.BrowseForFolder to give Default Folder and allow browse for any Folder

Threaded View

  1. #1
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    ShellApp.BrowseForFolder to give Default Folder and allow browse for any Folder

    ShellApp.BrowseForFolder to give Default Folder and allow browse for any Folder

    Hi
    _1) So in short: I want a code using the ShellApp.BrowseForFolder to give me a Dialogue box which allows
    Both
    me to browse and select a Folder ( from anywhere on my computer )
    and
    as well it having initially a default Initial Folder.

    I have been struggling to get that and have only been able to get one or the other.
    _....

    _2)
    In more detail to help explain exactly what I want:

    _ So I use often a code of this form ( Simplified to demo ) to select a Folder, whilst giving also a default suggestion.
    _It uses the VBA .FileDialog(msoFileDialogFolderPicker). Nice simple code works well
    Sub code1getFolderNameWithDottyFileDialog_msoFileDialogFolderPicker_Wonk()
    Dim FullFolderPath As String, FolderName As String
        With Application.FileDialog(msoFileDialogFolderPicker)
         .AllowMultiSelect = False
         .Title = "Folder To Export code to"
         .InitialFileName = "H:\Excel0202015Jan2016\ExcelForum\UserForm\AFolderForUserFormsAndCodeText\" ' Note strangely the extra "\" is required on the .InitialFileName even though that extra "\" never appears on any given string
         .Show '
         Debug.Print "You selected Folder Path""" & .SelectedItems(1) & """": MsgBox Prompt:="You selected Folder Path """ & .SelectedItems(1) & """"
         Let FullFolderPath = .SelectedItems(1)
         Let FolderName = Right(FullFolderPath, Len(FullFolderPath) - InStrRev(FullFolderPath, "\"))
         Debug.Print "You selected Folder """ & FolderName & """ to export code to  ": MsgBox Prompt:="You selected Folder """ & FolderName & """"
        End With
    End Sub
    (_... the only weird things I note is that on my default suggestion
    _a) the syntax is
    .InitialFileName =
    Which is a bit weird , I would have expected something like
    .InitialFolderName =
    Or better still
    .InitialFolderPath =
    _b) i have to add a “\" on the end of my “Initial File name, although that appears on none of my returned Strings..... _...but never mind those, unless out of interest you know why that is?.
    ..)

    _..............

    Now I am happy with that code above. But I would like an alternative, and have seen the ShellApp.BrowseForFolder often given.
    Generally I can get the ShellApp.BrowseForFolder version to work , but with a couple of problems .
    _ a) I cannot seem to get a form of the dialogue box to come up with
    both
    a default suggestion
    and
    the ability to browse for a Folder. I only seem to be able to get one of the other. Here then two examples,
    The first, code2a, gives you a default suggestion, but the ability to browse is lost. That makes of course this first code useless – you might as well hard copy the Folder name and be done!!!

    Sub code2a_getFolderNameWithShellAppDottyBrowseForFolder()
    Dim FullFolderPath As String, FolderName As String
    Dim ShellApp As Object 'These two lines are an Example of Late Binding". When using CreateObject, the registry is searched for a program identifier. You are creating a Late Bound reference to an application object returned by the "Application" property of the "Shell" object which is contained in the "Shell32" library.
    Set ShellApp = CreateObject("Shell.Application") '...is creating a "IShellDispatch4" object. Depending on your version, this may be IShellDispatch3 or IShellDispatch2. To see the properties and methods of this object, you will need to right click within your object browser and select, "Show Hidden Members".
                ''        Dim ShellApp  As Shell32.Shell ' The next two lines are the equivalent "Early Binding pair"
                ''        Set ShellApp = New Shell32.Shell ''You will need to do select form VB Editor options .. Extras...then scroll down to  Microsoft Shell Controls And Automation  ...  and add a check
    
    Dim objFolder As Object 'The  .BrowseForFolder Method appears either returns a string of the Folder name you choose, or an object which is that chosen Folder, depending on how you declare the variable to put the retuned "thing" in... so after doing these two sorts of declaration .......
    Set objFolder = ShellApp.BrowseForFolder(0, "Please choose a folder", 4000, "H:\Excel0202015Jan2016\ExcelForum\UserForm\AFolderForUserFormsAndCodeText") 'An Object of Folder type returned. Note: it will also take an extra "\" like  "H:\Excel0202015Jan2016\ExcelForum\UserForm\AFolderForUserFormsAndCodeText\", but it will not show it on the returned string !!
    Let FolderName = objFolder ' or below works as well
    Let FolderName = ShellApp.BrowseForFolder(0, "Please choose a folder", 4000, "H:\Excel0202015Jan2016\ExcelForum\UserForm\AFolderForUserFormsAndCodeText") 'A string of the name of the Folder is returned.      Note: it will also take an extra "\" like  "H:\Excel0202015Jan2016\ExcelForum\UserForm\AFolderForUserFormsAndCodeText\", but it will not show it on the returned string !!
    Let FullFolderPath = objFolder.self.path
    
    Debug.Print "You selected Folder Path""" & FullFolderPath & """": MsgBox Prompt:="You selected Folder Path """ & FullFolderPath & """"
    Debug.Print "You selected Folder """ & FolderName & """ to export code to  ": MsgBox Prompt:="You selected Folder """ & FolderName & """"
    
    End Sub
    _.......


    _b) Code2b allows you to browse, but I have no default suggestion.
    In addition, the Folder path returned , comes up sometimes in a peculiar form. For example if I select “Computer” as my Folder, then the correct Folder name is returned as
    “Computer”
    However the path returned comes out as
    "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"

    Sub code2b_getFolderNameWithShellAppDottyBrowseForFolder()
    Dim FullFolderPath As String, FolderName As String
    Dim ShellApp As Object
    Set ShellApp = CreateObject("Shell.Application")
    
    Dim objFolder As Object
    Set objFolder = ShellApp.BrowseForFolder(0, "Please choose a folder", 4000)
    Let FolderName = objFolder
    Let FolderName = ShellApp.BrowseForFolder(0, "Please choose a folder", 4000)
    Let FullFolderPath = objFolder.self.path
    
    Debug.Print "You selected Folder Path""" & FullFolderPath & """": MsgBox Prompt:="You selected Folder Path """ & FullFolderPath & """"
    Debug.Print "You selected Folder """ & FolderName & """ to export code to  ": MsgBox Prompt:="You selected Folder """ & FolderName & """"
    
    End Sub
    _.....

    So can anyone clear up this weirdaty, and give me a code which uses ShellApp.BrowseForFolder and gives me a dialogue box with a default Folder and the ability to browse. ( and returns maybe always the “normal” looking path )

    Thanks
    Alan

    _......
    _....

    P.s. The documentation is bad, or so my many hours of googling suggests.
    I did find some info here:

    http://wsh2.uw.hu/ch12f.html

    Summarised here are the options. But despite many hours of experimenting I could find no combination the gave my desired dialogue box...

    ' objDlg.BrowseForFolder(hWnd, Title, Options[, Root])
    '
    'The first parameter submits a window handle to the dialog box. You should always use the value 0 for this parameter because the script has no handle.
    '
    'The second parameter specifies the string shown in the dialog box below the title bar
    '
    'The third parameter is a 32-bit flag that specifies internal features of the dialog box.....
    '.... &H0001=1 Only file system folders can be selected. If this bit is set, the OK button is disabled if the user selects a folder that doesn't belong to the file system (such as the Control Panel folder).
    '.... &H0002=2 The user is prohibited from browsing below the domain within a network (during a computer search).
    '.... &H0004=4 Room for status text is provided under the text box. (I haven't found a way to show the status, however.)
    '.... &H0008= etc. Returns file system ancestors only.
    '.... &H0010 Shows an edit box in the dialog box for the user to type the name of an item.
    '.... &H0020 Validate the name typed in the edit box.
    '.... &H1000 Enables the user to browse the network branch of the shell's namespace for computer names.
    '.... &H2000 Enables the user to browse the network branch of the shell's namespace for printer names.
    '.... &H4000 Allows browsing for everything.
    '
    'The fourth parameter is optional and can be used to preselect a folder in the dialog box.
    ' You can submit a string with the folder's path (such as C:\Born) or you can use one of the values below to select a special folder object in the shell's namespace.
    ' .. 0 The Desktop (virtual) folder is the root directory. Using this constant along with &H0001 for the third parameter circumvents problems with the OK button.
    ' ..1 Internet Explorer is the root.
    ' ..2 The Programs folder of the Start menu is the root.
    ' ..3 The Control Panel folder is the root. The third parameter must be set to &H4000 (browse for everything).
    ' ..4 The Printers folder is the root. The third parameter must be set to &H4000 (browse for everything).
    ' ..5 The Documents folder of the Start menu is the root.
    ' ..6 The Favorites folder of the Start menu is the root.
    ' ..7 The Startup folder of the Start menu is the root. The third parameter must be set to &H4000 (browse for everything).
    ' ..8 The Recent folder is the root. The third parameter must be set to &H4000 (browse for everything).
    ' ..9 The SendTo folder is the root. The third parameter must be set to &H4000 (browse for everything).
    ' ..10 The Recycle Bin folder is the root. The third parameter must be set to &H4000 (browse for everything).
    ' ..11 The Start menu folder is the root.
    ' ..16 The Desktop (physical) folder is the root.
    ' ..17 My Computer is the root.
    ' ..18 Network Neighborhood is the root.
    ' ..19 The Nethood folder is the root.
    ' ..20 The Fonts folder is the root.
    ' ..21 The Templates folder is the root.


    Rem Ref
    ' http://www.excelforum.com/excel-prog...ny-folder.html
    ' http://www.mrexcel.com/forum/excel-q...orkbook-2.html
    ' http://www.mrexcel.com/forum/excel-q...xcel-file.html
    Last edited by Doc.AElstein; 05-27-2016 at 05:47 AM.
    '_- Google first, like this _ site:ExcelForum.com Gamut
    Use Code Tags: Highlight code; click on the # icon above,
    Post screenshots COPYABLE to a Spredsheet; NOT IMAGES PLEASE
    http://www.excelforum.com/the-water-...ml#post4109080
    https://app.box.com/s/gjpa8mk8ko4vkwcke3ig2w8z2wkfvrtv
    http://excelmatters.com/excel-forums/ ( Scrolll down to bottom )

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] VBA-Open At Specific Folder with BrowseForFolder
    By michaelisk in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-08-2013, 05:51 PM
  2. [SOLVED] vb macro browse folder
    By ccs_1981 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-19-2012, 05:46 AM
  3. Changing Folder from the default folder
    By cg7131 in forum Excel Programming / VBA / Macros
    Replies: 30
    Last Post: 08-29-2011, 04:56 PM
  4. Different 'Browse for Folder' dialog?
    By Jon.R in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-14-2011, 02:30 PM
  5. Browse for folder error
    By abousetta in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-29-2010, 05:26 PM
  6. Copy Folder Using Browse
    By davehunter in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-23-2007, 12:41 PM
  7. [SOLVED] Full folder path from BrowseForFolder
    By Jon in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-02-2006, 06:15 PM
  8. Browse for folder
    By Steph in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-08-2005, 12:55 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1