我将如何去显示访问2007 VBA中的打开文件(或文件选择)对话框?如何在Access 2007 VBA中显示“打开文件”对话框?
我已经尝试使用Application.GetOpenFileName,因为我会在Excel中,但该函数不存在于Access中。
我将如何去显示访问2007 VBA中的打开文件(或文件选择)对话框?如何在Access 2007 VBA中显示“打开文件”对话框?
我已经尝试使用Application.GetOpenFileName,因为我会在Excel中,但该函数不存在于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
。
下面是访问文档的例子:
' 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>工具>参考菜单)。
实际上,您可以使用后期绑定,并且不需要对11.0对象库的引用。 下面的代码将工作,而无需任何的引用: 昏暗˚F作为对象 集合F = Application.FileDialog(3) f.AllowMultiSelect =真 f.Show MSGBOX “文件choosen =” &f.SelectedItems .Count 请注意,以上运作良好,我的运行时也。 Albert D.Kallal 埃德蒙顿,加拿大 [email protected] – 2009-07-09 02:25:50
该示例从这里复制:http://support.microsoft.com/en-us/kb/824272 – Mike 2015-04-08 02:40:18
加入到什么艾伯特已经表示:
此代码(各种样品的混搭)提供有一个另存为对话框
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
我同意约翰·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的值将保持不变,因为多选是假的(只有一个项目是不断选择)。我在循环之外使用了它的价值,同样成功。不过,John M所做的做法可能更好。此外,文件夹选取器可用于获取选定的文件夹。我总是喜欢后期绑定,但我认为该对象是原生默认访问库,所以它可能没有必要在这里
我有一个类似的解决方案,以上,它适用于打开,保存,文件选择。我将它粘贴到它自己的模块中,并用在我创建的所有Access数据库中。如代码所述,它需要Microsoft Office 14.0 Object Library。我想是另一种选择:
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
+1始终希望这是可能的,关键是要使后期的绑定工作是通过数字选项,而不是msoOpenFileDialog等。这么简单但很好的答案:) – 2012-07-26 19:48:14