文件对话框错误访问VBA

时间:2014-12-09 17:28:48

标签: vba access-vba ms-access-2010 openfiledialog

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

错误:方法' FileDialog'对象' _Application'失败

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

所有代码:

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

我真的不确定如何解决这个问题。我检查了我的语法和所有内容。

5 个答案:

答案 0 :(得分:3)

我知道这是一个古老的问题,但是由于它实际上没有答案,而我今天需要一个答案,所以我会用我发现的东西来填写以防万一其他人也需要答案。

要解决此问题,您需要添加对&#34; Microsoft Office [yourversion]对象库&#34;的引用。在 Visual Basic编辑器&gt;&gt;工具&gt;&gt;引用...

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

VBA References Dialog

答案 1 :(得分:1)

我自己也尝试做同样的事情并找到了这个问题。我意识到它已经有一年多了。

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

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

答案 2 :(得分:0)

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

这是为了将16位代码移植到32位容易。

答案 3 :(得分:0)

有关使用FileDialogue.Show方法正确语法的更多信息,请查看http://msdn.microsoft.com/en-us/library/office/ff865217%28v=office.15%29.aspx。您的变量前面似乎需要Set

答案 4 :(得分:0)

如果您正在浏览一些很酷的UI,可以使用.NET wrapper 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

这里看起来像什么;

In Action

相关问题