VBA/Excel/Access/Word/File Path/FileDialog

Материал из VB Эксперт
Перейти к: навигация, поиск

Displays a dialog box that allows the user to select a directory. The selected directory name (or "Canceled") is then displayed by using the MsgBox function.

   <source lang="vb">

 Sub GetAFolder()
     With Application.FileDialog(msoFileDialogFolderPicker)
       .InitialFileName = Application.DefaultFilePath & "\"
       .Title = "Please select a location for the backup"
       .Show
       If .SelectedItems.Count = 0 Then
           MsgBox "Canceled"
       Else
           MsgBox .SelectedItems(1)
       End If
     End With
 End Sub
</source>
   
  


FileDialogFilters

   <source lang="vb">

    Private Sub cmdGetFile_Click()
        Dim fd As FileDialog
        Dim ffs As FileDialogFilters
        Dim vItem
        On Error GoTo Problem
        Set fd = Application.FileDialog(msoFileDialogOpen)
        With fd
            Set ffs = .Filters
            With ffs
              .clear
              .add "Pictures", "*.jpg"
            End With
            .AllowMultiSelect = True
            If .show = False Then Exit Sub
            For Each vItem In .SelectedItems
              Debug.Print vItem
            Next vItem
        End With
        Exit Sub

Problem:

        MsgBox "That was not a valid picture"
    End Sub
</source>
   
  


FileDialog with JPG file filter

   <source lang="vb">

    Private Sub cmdGetFile_Click()
        Dim fd As FileDialog
        Dim ffs As FileDialogFilters
        On Error GoTo Problem
        Set fd = Application.FileDialog(msoFileDialogOpen)
        With fd
            Set ffs = .Filters
            With ffs
              .clear
              .add "Pictures", "*.jpg"
            End With
            .AllowMultiSelect = False
            If .show = False Then Exit Sub
            Image1.Picture = LoadPicture(.SelectedItems(1))
        End With
        Exit Sub

Problem:

        MsgBox "That was not a valid picture"
    End Sub
</source>
   
  


Get selected paths

   <source lang="vb">

Public Sub ShowFileDialog()

   Dim fd As FileDialog
   Dim selectedPaths() As String
   Dim I As Integer
   Set fd = Application.FileDialog(msoFileDialogOpen)
   With fd     "Configure dialog box
       .AllowMultiSelect = True
       .FilterIndex = 2
       .Title = "Select Excel File(s)"
       .InitialFileName = ""
       "Show the dialog and collect file paths selected by the user
       If .Show = -1 Then   "User clicked Open
           ReDim selectedPaths(.SelectedItems.Count - 1)
           "Store file paths for later use.
           For I = 0 To .SelectedItems.Count - 1
               selectedPaths(I) = .SelectedItems(I + 1)
           Next I
       End If
       .Execute     "Open selected files
   End With
   Set fd = Nothing

End Sub

</source>
   
  


Open File Open Dialog and get the selection (Dialog Types Used with the FileDialog Object)

   <source lang="vb">

Dialog Type VBA Constant (FileDialogType) Open msoFileDialogOpen Save msoFileDialogSaveAs File Picker msoFileDialogFilePicker Folder Picker msoFileDialogFolderPicker Sub openDlg()

   Dim fc As FileDialogFilters
   Dim ff As FileDialogFilter
   Set fc = Application.FileDialog(msoFileDialogOpen).Filters
   Set ff = fc.Item(1)
   MsgBox ff.Description & ff.Extensions     "Displays "AllFiles" and *.*

End Sub

</source>
   
  


Set the AllowMultiSelect property of the dialog box to allow multiple selections in the dialog box

   <source lang="vb">

Sub OpenDialog()

   Dim dlgOpen As FileDialog
   Set dlgOpen = Application.FileDialog( _
       DialogType:=msoFileDialogOpen)
   With dlgOpen
        .AllowMultiSelect = True
        .Show
   End With
   msgBox dlgOpen.SelectedItems(1)

End Sub

</source>
   
  


The FileDialog Object

   <source lang="vb">

Sub SaveDialog()

   Dim dlgSaveAs As FileDialog
   Set dlgSaveAs = Application.FileDialog(DialogType:=msoFileDialogSaveAs)
   dlgSaveAs.Show
   msgBox dlgSaveAs.SelectedItems(1)

End Sub

</source>
   
  


User selects path to save HTML files

   <source lang="vb">

Private Sub cmdChangePath_Click()

   Dim fd As FileDialog
   Dim I As Integer
   Set fd = Application.FileDialog(msoFileDialogFolderPicker)
   With fd
       .AllowMultiSelect = False "Allow only one selection
       .Title = "Select Folder"
       .InitialFileName = ""
       If .Show = -1 Then
           MsgBox .SelectedItems(1)
       End If
   End With
   Set fd = Nothing

End Sub

</source>
   
  


Use the SelectedItems property of the FileDialog object to return the FileDialogSelectedItems collection.

   <source lang="vb">

Sub selected()

   Dim si As FileDialogSelectedItems
   Set si = Application.FileDialog(msoFileDialogOpen).SelectedItems

End Sub

</source>
   
  


You can use the Add method of the FileDialogFilters collection object to create your own list of filter

   <source lang="vb">

Sub fileDlg()

   Dim fd As FileDialog
   Dim imagePath As String
   Set fd = Application.FileDialog(msoFileDialogFilePicker)
   With fd
       .AllowMultiSelect = False
       .Filters.Clear
       .Filters.Add "All files", "*.*"
       .Filters.Add "Image", "*.jpg", 1
       .FilterIndex = 1
       .InitialFileName = ""
       .Title = "Select JPEG file"
       If .Show = -1 Then       "User pressed action button
           imagePath = .SelectedItems(1)
       End If
   End With

End Sub

</source>