Open Windows Explorer and select a file

前端 未结 1 1514
难免孤独
难免孤独 2020-12-08 10:15

Is there a way to open a Windows Explorer window from a vba form, navigate to a specific file and select it so that the file name is placed in a text box?

相关标签:
1条回答
  • 2020-12-08 10:50

    Check out this snippet:

    Private Sub openDialog()
        Dim fd As Office.FileDialog
    
        Set fd = Application.FileDialog(msoFileDialogFilePicker)
    
       With fd
    
          .AllowMultiSelect = False
    
          ' Set the title of the dialog box.
          .Title = "Please select the file."
    
          ' Clear out the current filters, and add our own.
          .Filters.Clear
          .Filters.Add "Excel 2003", "*.xls"
          .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
            txtFileName = .SelectedItems(1) 'replace txtFileName with your textbox
    
          End If
       End With
    End Sub
    

    I think this is what you are asking for.

    0 讨论(0)
提交回复
热议问题