vba 使用 FileDialogFolderPicker 时如何设置 .InitialView 并查看文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8191007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set .InitialView and see files when using FileDialogFolderPicker?
提问by sigil
I want open a dialog where the user can:
我想打开一个对话框,用户可以在其中:
- select a folder
- sort entries alphabetically
- see files as well as folders
- 选择一个文件夹
- 按字母顺序排列条目
- 查看文件和文件夹
I'm trying to use VBA's Application.FileDialog to accomplish this, current code as follows:
我正在尝试使用 VBA 的 Application.FileDialog 来完成此操作,当前代码如下:
Sub makeFileDialog()
Dim dialog As FileDialog
Dim result As String
Set dialog = Application.FileDialog(msoFileDialogFolderPicker)
With dialog
.InitialFileName = "c:\"
.InitialView = msoFileDialogViewDetails
If dialog.Show = -1 Then
result = .SelectedItems.Item(1)
Else
result = ""
End If
End With
debug.print result
End Sub
This lets me select a folder, but the .InitialView
isn't properly set--the views button is grayed out, and the folder list isn't divided into columns for sorting. I think maybe .InitialView
can't be set with msoFileDialogFolderPicker
. Also, no filenames show up.
这让我可以选择一个文件夹,但.InitialView
没有正确设置——视图按钮变灰,文件夹列表没有分成几列进行排序。我想也许.InitialView
不能用msoFileDialogFolderPicker
. 此外,没有显示文件名。
I tried changing FileDialog
's parameter to msoFileDialogFilePicker
, which let me use views and showed files, but with that I can't select and return a folder.
我尝试将FileDialog
的参数更改为msoFileDialogFilePicker
,这让我可以使用视图和显示文件,但是我无法选择和返回文件夹。
I saw a solution online that uses the CreateObject("Shell.Application")
, but the dialog created by the shell isn't very flexible and doesn't offer much information about each file.
我在网上看到了一个使用 的解决方案CreateObject("Shell.Application")
,但是 shell 创建的对话框不是很灵活,并且没有提供有关每个文件的太多信息。
Ideas?
想法?
回答by aevanko
Short answer: No. There is not way to allow a user to select either a folder or file. I think most would agree that this is a good thing.
简短回答:不可以。没有办法让用户选择文件夹或文件。我想大多数人都会同意这是一件好事。
You have it right - use msoFileDialogFilePickerto show all folders and files. You can, if you so desire, just use the filepicker, and parse the file name to get the folder path if you really need to.
你说得对 - 使用msoFileDialogFilePicker显示所有文件夹和文件。如果您愿意,您可以只使用文件选择器,并在确实需要时解析文件名以获取文件夹路径。