Excel中調用VBA選擇目標文件夾

  進行文件操作時,經常要用 VBA 選擇目標文件夾,現提供實現代碼:
  1.FileDialog 屬性
  Sub Sample1()
  With Application.FileDialog(msoFileDialogFolderPicker)
  If .Show = True Then
  MsgBox .SelectedItems(1)
  End If
  End With
  End Sub
  2.shell 方法
  Sub Sample2()
  Dim Shell, myPath
  Set Shell = CreateObject("Shell.Application")
  Set myPath = Shell.BrowseForFolder(&O0, "請選擇文件夾", &H1 + &H10, "G:")
  If Not myPath Is Nothing Then MsgBox myPath.Items.Item.Path
  Set Shell = Nothing
  Set myPath = Nothing
  End Sub