如何在Access 2007 VBA中显示"打开文件"对话框?

jwo*_*ard 34 ms-access access-vba

如何在Access 2007 VBA中显示打开的文件(或文件选择)对话框?

我已尝试使用Application.GetOpenFileName,就像在Excel中一样,但Access中不存在此功能.

Alb*_*lal 45

我对Renaud Bompuis答案的评论搞砸了.

实际上,您可以使用后期绑定,并且不需要对11.0对象库的引用.

以下代码无需任何引用即可使用:

 Dim f    As Object 
 Set f = Application.FileDialog(3) 
 f.AllowMultiSelect = True 
 f.Show 

 MsgBox "file choosen = " & f.SelectedItems.Count 
Run Code Online (Sandbox Code Playgroud)

请注意,上述内容在运行时也很有效.

  • +1总是希望这是可能的,使后期绑定工作的关键是传递数字选项而不是msoOpenFileDialog等.这么简单但是很好的答案:) (4认同)

Ren*_*uis 19

在Access 2007中,您只需要使用Application.FileDialog.

以下是Access文档中的示例:

' Requires reference to Microsoft Office 12.0 Object Library. '
Private Sub cmdFileDialog_Click()
   Dim fDialog As Office.FileDialog
   Dim varFile As Variant

   ' Clear listbox contents. '
   Me.FileList.RowSource = ""

   ' Set up the File Dialog. '
   Set fDialog = Application.FileDialog(msoFileDialogFilePicker)

   With fDialog

      ' Allow user to make multiple selections in dialog box '
      .AllowMultiSelect = True

      ' Set the title of the dialog box. '
      .Title = "Please select one or more files"

      ' Clear out the current filters, and add our own.'
      .Filters.Clear
      .Filters.Add "Access Databases", "*.MDB"
      .Filters.Add "Access Projects", "*.ADP"
      .Filters.Add "All Files", "*.*"

      ' Show the dialog box. If the .Show method returns True, the '
      ' user picked at least one file. If the .Show method returns '
      ' False, the user clicked Cancel. '
      If .Show = True Then

         'Loop through each file selected and add it to our list box. '
         For Each varFile In .SelectedItems
            Me.FileList.AddItem varFile
         Next

      Else
         MsgBox "You clicked Cancel in the file dialog box."
      End If
   End With
End Sub
Run Code Online (Sandbox Code Playgroud)

如示例所示,只需确保您具有对Microsoft Access 12.0对象库的引用(在VBE IDE>工具>引用菜单下).