VBA 呼叫資料夾選擇對話框有三種常見方式:API、Shell.Application、FileDialog。
Sub GetFolder_Shell()
Dim objShell As Object
Set objShell = CreateObject("Shell.Application")
Dim objFolder As Object
Set objFolder = objShell.BrowseForFolder(0, "選擇文件夾", 0, 0)
If Not objFolder Is Nothing Then MsgBox objFolder.self.path
End SubSub GetFolder_FileDialog()
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
If fd.Show = -1 Then MsgBox fd.SelectedItems(1)
End SubAPI 方式較複雜,但相容性佳;FileDialog 簡潔,適合 Office 環境。