Dav*_*ard 4 ms-access vba access-vba office-2016
在Access 2016中,我希望显示" 文件打开"对话框,允许用户选择要导入的CSV文件.但是,与线路相关的错误正在产生Dim FD as Office.FileDialog-
编译错误:未定义用户定义的类型
以下代码已从MSDN上发布的示例中复制(并稍微编辑).此示例被列为与Office 2013及更高版本相关,但代码中的第一个注释(与变量类型Office.FileDialog相关)似乎对此进行了谴责 -
需要引用Microsoft Office 11.0对象库.
当然,对于Office 2013,您需要引用MS Office 15对象库,然后引用相应的版本库以用于未来版本,例如2016?
但无论如何,在Access 2016中都没有引用Microsoft Office 11.0对象库.但是,它包含对Microsoft Access 16.0对象库的引用.
如何显示" 文件打开"对话框?
Function SelectFile(Optional ByVal title As String = "Please select a file", _
Optional ByVal allowMultiSelect As Boolean = False) As Variant
Dim FD As Office.FileDialog
Dim file As Variant
Set FD = Application.FileDialog(msoFileDialogFilePicker)
With FD
.title = "Please select a file" ' Add the dialog title
.allowMultiSelect = allowMultiSelect ' Set whether or not to allow multiple file selection
.filters.Clear ' Clear any existing filters
.filters.Add "CSV Files", "*.csv" ' Add new filters
'**
' Show the dialog to the user
'*
If .Show = True Then
For Each file In .selectedItems ' Grab the path/name of the selected file
SelectFile = file
Next
Else
SelectFile False
End If
End With
Set FD = Nothing ' Clean up the FD variable
End Function
Run Code Online (Sandbox Code Playgroud)
这是我目前选择的参考文献 -

以下是可用的MS Office referencs(没有参考Microsoft Office 16.0对象库) -

我不知道为什么Microsoft Office [版本]对象库没有显示在可用的引用中.但是,如果切换到后期绑定,则不需要它.
Const msoFileDialogFilePicker As Long = 3
'Dim FD As Office.FileDialog
Dim FD As Object
Dim file As Variant
Set FD = Application.FileDialog(msoFileDialogFilePicker)
Run Code Online (Sandbox Code Playgroud)
稍后,你需要决定在这里做什么......
For Each file In .selectedItems ' Grab the path/name of the selected file
SelectFile = file
Next
Run Code Online (Sandbox Code Playgroud)
当您使用AllowMultiSelect = True运行该代码并选择多个文件时,SelectFile将仅包含其中的最后一个.
| 归档时间: |
|
| 查看次数: |
13291 次 |
| 最近记录: |