lal*_*hka 5 ms-access vba access-vba
我看到这个主题如何在Access 2007 VBA中显示"打开文件"对话框?我喜欢那里没有使用引用的解决方案,但是,我无法弄清楚如何显示用户选择的文件路径.有人可以解释一下
非常感谢你
这是我正在谈论的那篇文章
Dim f As Object
Set f = Application.FileDialog(3)
f.AllowMultiSelect = True
f.Show
MsgBox "file choosen = " & f.SelectedItems.Count
Run Code Online (Sandbox Code Playgroud)
首先要做的事情是:你应该总是喜欢尽可能使用强类型变量.在这种情况下,您可以替换Object为Office.FileDialog.
要显示所选文件的路径,需要循环访问该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
Run Code Online (Sandbox Code Playgroud)
请注意FileDialog,如果需要自定义,还可以设置其他属性.例如,该.Title属性允许您指定将在标题栏中显示为对话框标题的标题.您还可以使用该.Filter属性指定过滤器,这将限制用户在对话框中可以查看和选择的文件类型.例如,如果要将选项限制为仅限Access数据库,则可以添加以下代码:
' Clear out the current filters
f.Filters.Clear
' Add a few custom filters
f.Filters.Add "Access Databases", "*.mdb"
f.Filters.Add "All Files", "*.*"
Run Code Online (Sandbox Code Playgroud)