Using the browse for file dialog in Access VBA

前端 未结 1 1105
野的像风
野的像风 2020-12-16 07:40

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

相关标签:
1条回答
  • 2020-12-16 07:50

    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", "*.*"
    
    0 讨论(0)
提交回复
热议问题