显示和使用文件对话框

FileDialog 对象允许显示 Access 使用的“文件”对话框,并确定用户选择了哪些文件。 FileDialog 对象的 SelectedItems 属性包含用户选择的文件的路径。 使用 For...每个 循环,可以枚举此集合并显示每个文件;SelectedItems 属性构造并返回集合上的迭代器。

以下示例将用户选定的文件添加到名为 FileList 的列表框。

注意

本示例需要引用 Microsoft Office 12.0 对象库。

Private Sub cmdFileDialog_Click() 
 
'Requires reference to Microsoft Office 12.0 Object Library. 
 
   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; *.ACCDB" 
      .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 the 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

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。