文件对话框错误访问 VBA

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

File Dialog error access VBA

vbaaccess-vbams-access-2010openfiledialog

提问by Bob

I am trying to have the file dialog box pop up so the user can select a file path to export a file in VBA but for some reason it throws this error on the following line of code.

我试图让文件对话框弹出,以便用户可以选择一个文件路径以在 VBA 中导出文件,但由于某种原因,它会在以下代码行中引发此错误。

Error: Method 'FileDialog' of object '_Application' failed

错误:对象“_Application”的方法“FileDialog”失败

Code: longResult = Application.FileDialog(msoFileDialogFolderPicker).Show

代码: longResult = Application.FileDialog(msoFileDialogFolderPicker).Show

All Code:

所有代码:

If choice = 6 Then

Dim intResult As Long
Dim strPath As String
'the dialog is displayed to the user
longResult = Application.FileDialog(msoFileDialogFolderPicker).Show
'checks if user has cancled the dialog
If intResult <> 0 Then
    'dispaly message box
Call MsgBox(Application.FileDialog(msoFileDialogFolderPicker _
    ).SelectedItems(1), vbInformation, "Selected Folder")
End If

Else

End

End If

I am really unsure how to fix this issue. I checked my syntax and everything.

我真的不确定如何解决这个问题。我检查了我的语法和一切。

回答by BlairH

Was trying to do the same thing myself and found this question. I realize that it is over a year old.

试图自己做同样的事情并发现了这个问题。我意识到它已经一年多了。

Try using the actual number (4) instead of msoFileDialogFolderPicker, that worked for me. I think something needs to be installed for the msoFileDialog constants to be initialized, when I tried printing any of the constants defined in the help file in the immediate window nothing was printed.

尝试使用实际数字 (4) 而不是 msoFileDialogFolderPicker,这对我有用。我认为需要安装一些东西才能初始化 msoFileDialog 常量,当我尝试在即时窗口中打印帮助文件中定义的任何常量时,没有打印任何内容。

Also, why does your code have one variable longResult and one variable intResult?

另外,为什么你的代码有一个变量 longResult 和一个变量 intResult?

回答by John Sherwood

I know that this is a bit of an old question at this point, but since it doesn't actually have the answer, and I needed one today, I'm going to chime in with what I found just in case anyone else needs the answer too.

我知道这在这一点上是一个老问题,但由于它实际上没有答案,而我今天需要一个,我将补充我发现的内容,以防其他人需要也回答。

To fix this you need to add a reference to "Microsoft Office [yourversion] Object Library" in Visual Basic Editor >> Tools >> References...

要解决此问题,您需要在Visual Basic 编辑器 >> 工具 >> 参考...中添加对“Microsoft Office [您的版本] 对象库”的引用。

The dialog in question should look like this:

有问题的对话框应如下所示:

VBA References Dialog

VBA 引用对话框

回答by Krish

If you are after some cool UI, you can checkout my Githubfor sample database using .NET wrapper dll. Which allows you to simply call a function and to open filedialog with file-drag-and-dropfunction

如果您想要一些很酷的 UI,您可以使用 .NET 包装器 dll检查我的Github以获取示例数据库。它允许你简单地调用一个函数,并打开与FileDialog的文件拖放和拖放功能

Dim FilePaths As String
    FilePaths = gDll.DLL.ShowDialogForFile("No multiple files allowed", False)
'Will return a JSONArray string.
'Multiple files can be opend by setting AllowMulti:=true

here what it looks like;

这是它的样子;

In Action

在行动

回答by BambiLongGone

Almost nothing is an integer in VB6 as integer is a VB4 16 bit type. Win32 Integers are called Long in VB6/VBA.

VB6 中几乎没有整数,因为整数是 VB4 16 位类型。Win32 整数在 VB6/VBA 中称为 Long。

This was to make porting 16 bit code to 32 bit easy.

这是为了便于将 16 位代码移植到 32 位。

回答by Chrismas007

Check out http://msdn.microsoft.com/en-us/library/office/ff865217%28v=office.15%29.aspxfor more information on proper syntax with FileDialogue.Show method. It appears you need a Setin front of your variable.

查看http://msdn.microsoft.com/en-us/library/office/ff865217%28v=office.15%29.aspx以获取有关 FileDialogue.Show 方法正确语法的更多信息。看来你需要Set在你的变量前面。