【问题标题】:FileDialog(msoFileDialogFolderPicker) - how to set initial path to "root" / "This PC"?FileDialog(msoFileDialogFolderPicker) - 如何将初始路径设置为“root”/“This PC”?
【发布时间】:2021-02-18 16:09:03
【问题描述】:

如果.InitialFileName 未设置,“选择文件夹”对话框FileDialog(msoFileDialogFolderPicker) 使用应用程序的当前目录

有没有办法将对话框强制到 Windows 资源管理器中的“根”文件夹(Windows 10 中的“这台电脑”,早期版本中的“我的电脑”)?


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 使用幻数 17 来指定:

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

我不喜欢用BrowseForFolder,因为如果指定了一个初始文件夹,用户就被限制在这个文件夹及以下。

【问题讨论】:

  • 不确定是否可以在 VBA 中完成,因为“我的电脑”并不是真正要传递给 .InitialFileName 字符串的目录。根据提供的初始路径选择对话的混合解决方案怎么样?如果为空,则显示另一个。
  • 是的,恐怕你是对的。混合解决方案实际上是一个有趣的想法 - 尽管它可能会让一些用户感到困惑。
  • 魔法17是ShellSpecialFolderConstantsssfDRIVES
  • 嗯...正如已删除的评论中所说,不幸的是,这需要 Common Item Dialog 代码,并且不能与 Application.FileDialog 所基于的旧 CommonDialog 代码一起使用。 Common Item Dialog 是 COM 代码,但没有实现 IDispatch,这会导致一个主要问题 (context),解决这个问题需要弄乱 vtables 或对 TLB 进行逆向工程 (resource)。明智的做法是在 C# 中创建一个 COM 对象。
  • 为什么不直接使用C:\

标签: excel vba ms-access


【解决方案1】:

所以显然Application.FileDialog 是不可能的。

我应用了 Kostas 的建议,并在一个函数中实现了这两种方法(FileDialog 和 Shell.BrowseForFolder),具体取决于是否将初始路径传递给它。

查看内联 cmets。这是我的最终版本。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-06
    • 2012-01-22
    • 2012-05-18
    • 2022-08-19
    • 2018-03-22
    • 1970-01-01
    • 2016-04-21
    • 1970-01-01
    相关资源
    最近更新 更多