MS Access中未定义"FileDialog"类型 [英] 'FileDialog' type is not defined in MS Access

查看:363
本文介绍了MS Access中未定义"FileDialog"类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Access 2016中,我希望显示打开文件对话框,允许用户选择要导入的CSV文件.但是,相对于行Dim FD as Office.FileDialog-

In Access 2016, I wish to display the File Open dialog, allowing users to select a CSV file to import. However, an error is being generated in relation to the line Dim FD as Office.FileDialog -

编译错误:未定义用户定义类型

Compile error: User-defined type not defined

以下代码已从示例发布在 MSDN 上.此示例被列为与Office 2013及更高版本相关,但是代码中的第一个注释(与变量类型 Office.FileDialog 相关)似乎对此很困惑-

The below code has been copied (and edited slightly) from the example posted on MSDN. This example is listed as relevant for Office 2013 and later, yet the very first comment in the code (in relation to the variable type Office.FileDialog) seems to contridict this -

需要引用Microsoft Office 11.0对象库.

Requires reference to Microsoft Office 11.0 Object Library.

对于Office 2013,您肯定需要引用MS Office 15对象库,然后引用相应的版本库以用于将来的版本(例如2016)?

Surely for Office 2013 you'd need to refernece the MS Office 15 Object Library, and then the respective version library for future versions, such as 2016?

但是,无论如何,在Access 2016中没有引用Microsoft Office 11.0对象库.但是,其中包含对 Microsoft Access 16.0对象库的引用.

But regardless, in Access 2016 there is no refernece to the Microsoft Office 11.0 Object Library. There is however a reference to the Microsoft Access 16.0 Object Library, which is included.

如何显示打开文件对话框?

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

这是我当前选择的参考资料-

Here are my currently selected references -

这是可用的MS Office参考(不参考 Microsoft Office 16.0对象库)-

And here are the available MS Office referencs (no refernece to Microsoft Office 16.0 Object Library) -

推荐答案

我不知道为什么在可用引用中没有显示 Microsoft Office [版本]对象库.但是,如果您切换到后期绑定,则不需要它.

I don't know why Microsoft Office [version] Object Library is not displayed among the available references. However, you don't need it if you switch to late binding.

Const msoFileDialogFilePicker As Long = 3
'Dim FD As Office.FileDialog
Dim FD As Object
Dim file As Variant
Set FD = Application.FileDialog(msoFileDialogFilePicker)

稍后,您需要决定在这里做什么...

Later on, you'll need to decide what to do here ...

For Each file In .selectedItems  ' Grab the path/name of the selected file
    SelectFile = file
Next

当使用 AllowMultiSelect = True 运行该代码并选择多个文件时,SelectFile将仅包含它们中的最后一个.

When you run that code with AllowMultiSelect = True, and select multiple files, SelectFile will contain only the last of them.

这篇关于MS Access中未定义"FileDialog"类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆