Wie gehe ich vor, um in Access 2007 VBA ein Dialogfeld zum Öffnen einer Datei (oder zum Auswählen einer Datei) anzuzeigen?
Ich habe versucht, Application.GetOpenFileName wie in Excel zu verwenden, aber diese Funktion ist in Access nicht vorhanden.
Meine Kommentare zu Renaud Bompuis 'Antwort sind durcheinander.
Tatsächlich können Sie die späte Bindung verwenden, und der Verweis auf die 11.0-Objektbibliothek ist nicht erforderlich.
Der folgende Code funktioniert ohne Referenzen:
Dim f As Object
Set f = Application.FileDialog(3)
f.AllowMultiSelect = True
f.Show
MsgBox "file choosen = " & f.SelectedItems.Count
Beachten Sie, dass das oben Genannte auch zur Laufzeit gut funktioniert.
In Access 2007 müssen Sie nur Application.FileDialog
.
Hier ist das Beispiel aus der Access-Dokumentation:
' 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
Vergewissern Sie sich, wie im Beispiel angegeben, dass Sie einen Verweis auf Microsoft Access 12.0 Object Library (im Menü VBE IDE> Tools> References)) haben.
Ergänzung zu dem, was Albert bereits gesagt hat:
Dieser Code (ein Mashup verschiedener Beispiele) bietet die Möglichkeit, ein Dialogfeld "Speichern unter" aufzurufen
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
Ich habe eine ähnliche Lösung wie oben und sie funktioniert zum Öffnen, Speichern und Auswählen von Dateien. Ich füge es in ein eigenes Modul ein und verwende es in allen Access-DBs, die ich erstelle. Wie im Code angegeben, ist die Microsoft Office 14.0-Objektbibliothek erforderlich. Nur eine andere Option, nehme ich an:
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
Ich stimme zu, John M hat die beste Antwort auf die Frage von OP. Obwohl nicht explizit angegeben, besteht der offensichtliche Zweck darin, einen ausgewählten Dateinamen zu erhalten, während andere Antworten entweder Zählungen oder Listen zurückgeben. Ich würde jedoch hinzufügen, dass der msofiledialogfilepicker in diesem Fall eine bessere Option sein könnte. dh:
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
Hinweis: Der Wert von varfile bleibt gleich, da multiselect false ist (es wird immer nur ein Element ausgewählt). Ich habe seinen Wert außerhalb der Schleife mit gleichem Erfolg verwendet. Es ist jedoch wahrscheinlich besser, es so zu machen, wie es John M getan hat. Die Ordnerauswahl kann auch verwendet werden, um einen ausgewählten Ordner abzurufen. Ich bevorzuge immer das späte Binden, denke aber, dass das Objekt in der Standardzugriffsbibliothek enthalten ist, sodass es hier möglicherweise nicht erforderlich ist