【发布时间】: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是ShellSpecialFolderConstants的ssfDRIVES。
-
为什么不直接使用
C:\?