在 Access VBA 中使用浏览文件对话框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4813598/
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
Using the browse for file dialog in Access VBA
提问by lalachka
i saw this topic How to show "Open File" Dialog in Access 2007 VBA?and i like the solution there that's not using references, however, i can't figure out how to display the file path that the user selected. can someone please explain
我看到了这个主题如何在 Access 2007 VBA 中显示“打开文件”对话框?我喜欢那里不使用引用的解决方案,但是,我不知道如何显示用户选择的文件路径。有人可以解释一下吗
thank you very much
非常感谢您
this is the piece i'm talking about
这是我要说的那件作品
Dim f As Object
Set f = Application.FileDialog(3)
f.AllowMultiSelect = True
f.Show
MsgBox "file choosen = " & f.SelectedItems.Count
回答by Cody Gray
First things first: you should always prefer to use strongly-typed variables whenever possible. In this case, you can replace Objectwith Office.FileDialog.
第一件事:你应该总是尽可能地使用强类型变量。在这种情况下,您可以替换Object为Office.FileDialog。
To display the paths of each file that was selected, you need to loop through the SelectedItemscollection. For example, you would add the following code:
要显示选定的每个文件的路径,您需要遍历SelectedItems集合。例如,您将添加以下代码:
Dim f As Office.FileDialog
Set f = Application.FileDialog(3)
f.AllowMultiSelect = True
' Show the dialog. If the method returns True, the user picked at least one file.
' If the method returns False, the user clicked Cancel.
If f.Show Then
MsgBox f.SelectedItems.Count & " file(s) were chosen."
' Display the full path to each file that was selected
Dim i As Integer
For i = 1 To f.SelectedItems.Count
MsgBox f.SelectedItems(i)
Next i
End If
Note that the FileDialogalso has other properties that you can set, if you require customization. For example, the .Titleproperty allows you to specify a title that will appear as the dialog's caption in the title bar. You can also specify a filter using the .Filterproperty, which will limit the type of files that the user will be able to see and choose from in the dialog. For example, if you wanted to limit the choices to only Access Databases, you could add the following code:
请注意FileDialog,如果您需要自定义,还可以设置其他属性。例如,该.Title属性允许您指定一个标题,该标题将在标题栏中显示为对话框的标题。您还可以使用.Filter属性指定过滤器,这将限制用户能够在对话框中查看和选择的文件类型。例如,如果您想将选项限制为仅访问数据库,您可以添加以下代码:
' Clear out the current filters
f.Filters.Clear
' Add a few custom filters
f.Filters.Add "Access Databases", "*.mdb"
f.Filters.Add "All Files", "*.*"

