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

2019-05-17 08:35发布

我怎么会去显示访问2007 VBA打开的文件(或文件选择)对话框?

我一直在使用Application.GetOpenFileName我会在Excel中尝试过,但这个功能不会访问存在。

Answer 1:

我在雷诺Bompuis的答复意见搞砸。

其实,你可以使用后期绑定,不需要参照11.0对象库。

下面的代码将工作没有任何引用:

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

 MsgBox "file choosen = " & f.SelectedItems.Count 

需要注意的是,上述工作良好运行时也。



Answer 2:

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

下面是从访问文档的例子:

' 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

由于样本说,只要确保您有对Microsoft Access 12.0对象库的引用(VBE IDE>工具>参考菜单下)。



Answer 3:

除了什么艾伯特已经表示:

这个代码(各种样品的混搭)提供了具有另存为对话框的能力

Function getFileName() As String
    Dim fDialog    As Object
    Set fDialog = Application.FileDialog(msoFileDialogSaveAs)
    Dim varFile As Variant

    With fDialog
       .AllowMultiSelect = False
       .Title = "Select File Location to Export XLSx :"
       .InitialFileName = "jeffatwood.xlsx"

    If .Show = True Then
       For Each varFile In .SelectedItems
         getFileName = varFile
       Next
    End If
End With
End Function


Answer 4:

我有一个类似的解决方案上面,它适用于打开,保存,文件选择。 我把它贴到了自己的模块,并在所有的Access数据库的创建使用。 由于代码指出它需要的Microsoft Office 14.0对象库。 只是另一种选择,我想:

Public Function Select_File(InitPath, ActionType, FileType)
    ' Requires reference to Microsoft Office 14.0 Object Library.

    Dim fDialog As Office.FileDialog
    Dim varFile As Variant


    If ActionType = "FilePicker" Then
        Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
        ' Set up the File Dialog.
    End If
    If ActionType = "SaveAs" Then
        Set fDialog = Application.FileDialog(msoFileDialogSaveAs)
    End If
    If ActionType = "Open" Then
        Set fDialog = Application.FileDialog(msoFileDialogOpen)
    End If
    With fDialog
        .AllowMultiSelect = False
        ' Disallow user to make multiple selections in dialog box
        .Title = "Please specify the file to save/open..."
        ' Set the title of the dialog box.
        If ActionType <> "SaveAs" Then
            .Filters.Clear
            ' Clear out the current filters, and add our own.
            .Filters.Add FileType, "*." & FileType
        End If
        .InitialFileName = InitPath
        ' Show the dialog box. If the .Show method returns True, the
        ' user picked a 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
                'return the subroutine value as the file path & name selected
                Select_File = varFile
            Next
        End If
    End With
End Function


Answer 5:

我同意约翰·M有最佳答案OP的问题。 想过没有显式声明,明显的目的是为了获得一个选定的文件名,而其他的答案返回要么计数或列表。 不过,我想补充的是,msofiledialogfilepicker可能是在这种情况下一个更好的选择。 即:

Dim f As object
Set f = Application.FileDialog(msoFileDialogFilePicker)
dim varfile as variant 
f.show
with f
    .allowmultiselect = false
     for each varfile in .selecteditems
        msgbox varfile
     next varfile
end with

注:varfile的值将保持不变,因为多选是假的(只有一个项目是不断选择)。 我用同样的成功环路以外的价值。 这可能是更好的做法,这样做是约翰·中号没有,但是。 此外,该文件夹选择器可用于获取选定的文件夹。 我总是喜欢后期绑定,但我认为对象是原产于默认访问库,因此它可能没有必要在这里



文章来源: How to show “Open File” Dialog in Access 2007 VBA?