FileDialog(msoFileDialogFolderPicker) - how to set initial path to "root" / "This PC"?

Andre

If .InitialFileName isn't set, the "Select Folder" dialog FileDialog(msoFileDialogFolderPicker) uses the current directory of the application.

Is there any way to force the dialog to the "root" folder in Windows explorer ("This PC" in Windows 10, "My Computer" in earlier versions) ?


Public Function GetFolderName(InitPath As String) As String

    With Application.FileDialog(msoFileDialogFolderPicker)

        If InitPath <> "" Then
            If Right$(InitPath, 1) <> "\" Then
                InitPath = InitPath & "\"
            End If
            .InitialFileName = InitPath
        Else
            .InitialFileName = ""   ' <-- What can I put here to start at "This PC" ?
        End If
        
        If .Show() = True Then
            If .SelectedItems.Count > 0 Then
                GetFolderName = .SelectedItems(1)
            End If
        End If

    End With

End Function

Shell.Application.BrowseForFolder uses the magic number 17 to specify this:

? CreateObject("Shell.Application").BrowseForFolder(0, "", &H11, 17).Self.Path

I don't like to use BrowseForFolder, because if an initial folder is specified, the user is limited to this folder and below.

Andre

So apparently this is not possible with Application.FileDialog.

I applied Kostas' suggestion and implemented both methods (FileDialog and Shell.BrowseForFolder) in one function, depending on whether an initial path is passed to it.

See inline comments. This is my final version.

Public Function GetFolderName(sCaption As String, InitPath As String) As String

    Dim sPath As String
    
    ' "Hybrid" approach:
    ' If InitPath is set, use Application.FileDialog because it's more convenient for the user.
    ' If not, we want to open the Folder dialog at "This PC", which is not possible with Application.FileDialog
    '   => then use Shell.Application.BrowseForFolder
    
    If InitPath <> "" Then
    
        With Application.FileDialog(msoFileDialogFolderPicker)
        
            .Title = sCaption
            ' FileDialog needs the init path to end with \ or it will select the parent folder
            If Right$(InitPath, 1) <> "\" Then
                InitPath = InitPath & "\"
            End If
            .InitialFileName = InitPath
            
            If .Show() = True Then
                If .SelectedItems.Count > 0 Then
                    sPath = .SelectedItems(1)
                End If
            End If
            
        End With
        
    Else
        
        ' https://ss64.com/vb/browseforfolder.html  has all the flags and constants
        Const BIF_RETURNONLYFSDIRS = &H1    ' default
        Const BIF_EDITBOX = &H10            ' allow users to paste a path e.g. from Explorer
        Const BIF_NONEWFOLDER = &H200       ' use this if users shouldn't be able to create folders from this dialog

        Dim oShell As Object
        Dim oFolder As Object

        Set oShell = CreateObject("Shell.Application")
        ' 17 = ssfDRIVES  is "This PC"
        Set oFolder = oShell.BrowseForFolder(0, sCaption, BIF_RETURNONLYFSDIRS + BIF_EDITBOX, 17)
        
        If Not oFolder Is Nothing Then
            ' .Self gets FolderItem from Folder object
            ' https://devblogs.microsoft.com/scripting/how-can-i-show-users-a-dialog-box-that-only-lets-them-select-folders/
            sPath = oFolder.Self.Path
            
            If Left$(sPath, 2) = "::" Then
                sPath = ""       ' User tricked the dialog into returning a GUID - invalid!
            End If
        End If
        
    End If
    
    GetFolderName = sPath

End Function

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

FileDialog(msoFileDialogFolderPicker) - how to set initial path to "root" / "This PC"?

From Dev

How to set a path variable without root acess?

From Dev

HTML: how to set root "/" path according to base

From Dev

how to set a grails 3 app to use a root context path?

From Dev

How to set context path to root("/") in Tomcat 7.0 when using Maven

From Dev

How to set the root of git repository to vi/vim find path?

From Dev

How to set Android Home in Ubuntu when the path is in the root folder

From Dev

How do I set root path to '/' without controller and action in Rails?

From Dev

How to set path of the file to its root in OpenFileDialogBox C#

From Dev

How to set the root of git repository to vi/vim find path?

From Dev

How to set STATIC_ROOT and MEDIA_ROOT so the path used is not hard coded?

From Dev

How can I set that only root + a given user can shut down my pc?

From Dev

How to set initial color for farbtastic?

From Dev

how to set initial value in react?

From Dev

How to set initial view properties?

From Dev

How to set initial color for farbtastic?

From Dev

how to set initial value in react?

From Dev

Set path from DOCUMENT_ROOT in <file>

From Dev

Paperclip, set path outside of rails root folder

From Dev

django - urls.py root path set

From Dev

Set path from DOCUMENT_ROOT in <file>

From Dev

How to get GNU `find` to not display the initial path?

From Dev

How to get GNU `find` to not display the initial path?

From Dev

How get root url path

From Dev

How to find Root Path Name

From Dev

How do I set my foo.war's root path to be /foo/bar instead of /foo?

From Dev

How do I set my foo.war's root path to be /foo/bar instead of /foo?

From Dev

How to split path to set of Path

From Dev

How to set initial value of a custom slider in qml?

Related Related

  1. 1

    FileDialog(msoFileDialogFolderPicker) - how to set initial path to "root" / "This PC"?

  2. 2

    How to set a path variable without root acess?

  3. 3

    HTML: how to set root "/" path according to base

  4. 4

    how to set a grails 3 app to use a root context path?

  5. 5

    How to set context path to root("/") in Tomcat 7.0 when using Maven

  6. 6

    How to set the root of git repository to vi/vim find path?

  7. 7

    How to set Android Home in Ubuntu when the path is in the root folder

  8. 8

    How do I set root path to '/' without controller and action in Rails?

  9. 9

    How to set path of the file to its root in OpenFileDialogBox C#

  10. 10

    How to set the root of git repository to vi/vim find path?

  11. 11

    How to set STATIC_ROOT and MEDIA_ROOT so the path used is not hard coded?

  12. 12

    How can I set that only root + a given user can shut down my pc?

  13. 13

    How to set initial color for farbtastic?

  14. 14

    how to set initial value in react?

  15. 15

    How to set initial view properties?

  16. 16

    How to set initial color for farbtastic?

  17. 17

    how to set initial value in react?

  18. 18

    Set path from DOCUMENT_ROOT in <file>

  19. 19

    Paperclip, set path outside of rails root folder

  20. 20

    django - urls.py root path set

  21. 21

    Set path from DOCUMENT_ROOT in <file>

  22. 22

    How to get GNU `find` to not display the initial path?

  23. 23

    How to get GNU `find` to not display the initial path?

  24. 24

    How get root url path

  25. 25

    How to find Root Path Name

  26. 26

    How do I set my foo.war's root path to be /foo/bar instead of /foo?

  27. 27

    How do I set my foo.war's root path to be /foo/bar instead of /foo?

  28. 28

    How to split path to set of Path

  29. 29

    How to set initial value of a custom slider in qml?

HotTag

Archive