進行文件操作時,經常要用 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
新聞熱點
疑難解答