i saw this topic How to show "Open File" Dialog in Access 2007 VBA? and i like the solution there that's not using references, however, i can't figure out how to display the file path that the user selected. can someone please explain
thank you very much
this is the piece i'm talking about
Dim f As Object
Set f = Application.FileDialog(3)
f.AllowMultiSelect = True
f.Show
MsgBox "file choosen = " & f.SelectedItems.Count
First things first: you should always prefer to use strongly-typed variables whenever possible. In this case, you can replace Object
with Office.FileDialog
.
To display the paths of each file that was selected, you need to loop through the SelectedItems
collection. For example, you would add the following code:
Dim f As Office.FileDialog
Set f = Application.FileDialog(3)
f.AllowMultiSelect = True
' Show the dialog. If the method returns True, the user picked at least one file.
' If the method returns False, the user clicked Cancel.
If f.Show Then
MsgBox f.SelectedItems.Count & " file(s) were chosen."
' Display the full path to each file that was selected
Dim i As Integer
For i = 1 To f.SelectedItems.Count
MsgBox f.SelectedItems(i)
Next i
End If
Note that the FileDialog
also has other properties that you can set, if you require customization. For example, the .Title
property allows you to specify a title that will appear as the dialog's caption in the title bar. You can also specify a filter using the .Filter
property, which will limit the type of files that the user will be able to see and choose from in the dialog. For example, if you wanted to limit the choices to only Access Databases, you could add the following code:
' Clear out the current filters
f.Filters.Clear
' Add a few custom filters
f.Filters.Add "Access Databases", "*.mdb"
f.Filters.Add "All Files", "*.*"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With