vba 文件对话框不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9476268/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 12:41:18  来源:igfitidea点击:

FileDialog doesn't work

vbams-accessms-access-2010

提问by Emily

I've looked quite intensely, but couldn't find a post that directly solves my problem.

我看了很紧张,但找不到直接解决我的问题的帖子。

The following code for a form I created works in Access 2003, which I use at work.

我创建的表单的以下代码适用于我在工作中使用的 Access 2003。

Dim FileName As FileDialog
Set FileName = Application.FileDialog(msoFileDialogFilePicker)
Dim Name As Variant

With FileName
    .AllowMultiSelect = False
    .Show
    If .SelectedItems.Count = 0 Then
    MsgBox "No file selected."
    Exit Sub
    End If
End With

For Each Name In FileName.SelectedItems
FileNameTextBox.Text = Mid$(Name, InStrRev(Name, "\") + 1)
Next Name

However, when I tried to run the same code on a form in Access 2010 on my personal computer, it doesn't work.The error message highlights the first line and says "User-defined type not defined." I also tried declaring FileName as Office.FileDialog, but no luck either. I do have Microsoft Access 14.0 Object Library as one of the references in use, so I don't know what's wrong with that.

但是,当我尝试在个人计算机上的 Access 2010 中的表单上运行相同的代码时,它不起作用。错误消息突出显示第一行并显示“未定义用户定义的类型”。我也尝试过声明FileName as Office.FileDialog,但也没有运气。我确实有 Microsoft Access 14.0 Object Library 作为正在使用的引用之一,所以我不知道这有什么问题。

I've only been using Access for two weeks, and all my knowledge come from googling, so it's very likely that I'm missing something obvious.

我只使用 Access 两个星期,我所有的知识都来自谷歌搜索,所以我很可能遗漏了一些明显的东西。

回答by HansUp

The FileDialogobject is not provided by the Access library, but by the Office library. So your code should work if you set a reference to the Microsoft Office [version number] Object Library. Either you don't have that reference set, or it's broken.

FileDialog对象不是由 Access 库提供的,而是由 Office 库提供的。因此,如果您设置对Microsoft Office [版本号] 对象库的引用,您的代码应该可以工作。要么你没有那个参考集,要么它坏了。

However if it were me, I would leave the reference unset and modify the code like this. See if it works for you.

但是,如果是我,我会不设置引用并像这样修改代码。看看它是否适合你。

Const msoFileDialogFilePicker As Long = 3
Dim objDialog As Object

Set objDialog = Application.FileDialog(msoFileDialogFilePicker)

With objDialog
    .AllowMultiSelect = False
    .Show
    If .SelectedItems.Count = 0 Then
        MsgBox "No file selected."
    Else
        Me.FileNameTextBox.Value = Dir(.SelectedItems(1))
    End If
End With

回答by Emily

In tools, references..., you have to select "Microsoft Office14.0 Object Library" instead of the Microsoft Access one.

在工具、参考...中,您必须选择“Microsoft Office14.0 Object Library”而不是 Microsoft Access。