如何在Access 2007 VBA中显示“打开文件”对话框?
如何在Access 2007 VBA中显示打开的文件(或文件select)对话框?
我已经尝试使用Application.GetOpenFileName,因为我会在Excel中,但此function不存在于Access中。
我对Renaud Bompuis的回答的评论搞砸了。
实际上,您可以使用后期绑定,而不需要对11.0对象库的引用。
以下代码将不带任何引用:
Dim f As Object Set f = Application.FileDialog(3) f.AllowMultiSelect = True f.Show MsgBox "file choosen = " & f.SelectedItems.Count
请注意,上面的运行时也很好。
在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
如示例所示,只要确保您有一个对Microsoft Access 12.0对象库 (在VBE IDE>工具>引用菜单下)的引用。
除了Albert已经说过的话:
此代码(各种样本的混搭)提供了具有SaveAs对话框的function
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
我有一个类似的解决scheme,以上,它的作品打开,保存,文件select。 我把它粘贴到它自己的模块中,并用在我创build的所有Access数据库中。 如代码所述,它需要Microsoft Office 14.0 Object Library。 我想是另一种select:
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
我同意John M对OP的问题有最好的回答。 思想没有明确说明,明显的目的是得到一个选定的文件名,而其他答案返回计数或列表。 然而,我会补充说,在这种情况下,msofiledialogfilepicker可能是更好的select。 即:
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
注意:由于multiselect为false,因此varfile的值将保持不变(只select一个项目)。 我在循环之外使用了它的价值,同样成功 不过,John M所做的做法可能更好。 此外,文件夹选取器可用于获取选定的文件夹。 我总是喜欢后期绑定,但我认为这个对象是默认访问库的本地对象,所以在这里可能没有必要