'注意引用 microsoft office 10.0 (或以上) object library
'在檔案對話方塊對中返回選擇一個檔案夾的路徑.
Public Function ChooseFolder() As String
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(msoFileDialogFolderPicker)
With dlgOpen
If .Show = -1 Then
ChooseFolder = .SelectedItems(1)
End If
End With
Set dlgOpen = Nothing
End Function
'--------------------------------------------------------
'在檔案對話方塊對中,選擇一個檔案。
Public Function ChooseOneFile(Optional TitleStr As String
= "選擇你要的檔案", Optional TypesDec As String = "所有檔案",
Optional Exten As String = "*.*") As String
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(msoFileDialogFilePicker)
With dlgOpen
.Title = TitleStr
.Filters.Clear '清除所有的檔案類型.
.Filters.Add TypesDec, Exten
.AllowMultiSelect = False '不能多選.
If .Show = -1 Then
' .AllowMultiSelect = True '多個檔案
' For Each vrtSelectedItem In .SelectedItems
' MsgBox "Path name: " & vrtSelectedItem
' Next vrtSelectedItem
ChooseOneFile = .SelectedItems(1) '第一個檔案
End If
End With
Set dlgOpen = Nothing
End Function