打开Windows资源管理器并选择一个文件

时间:2012-04-24 19:47:56

标签: vba excel-vba excel

有没有办法从vba表单打开Windows资源管理器窗口,导航到特定文件并选择它以便将文件名放在文本框中?

1 个答案:

答案 0 :(得分:71)

查看此代码段:

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

我认为这就是你所要求的。