標題:
[發問]
此段程式中如何加一個判斷?
[打印本頁]
作者:
maiko
時間:
2012-10-16 17:23
標題:
此段程式中如何加一個判斷?
Sub OpenPath()
Dim fd As FileDialog, FileName As String, FileFolder As String
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
With fd
.AllowMultiSelect = False
If .Show = -1 Then
FileFolder = .SelectedItems(1)
Else
End
End If
End With
Set fd = Nothing
End Sub
複製代碼
如何加一個判斷如果沒有選取時就不能離開FileDialog?Thanks.
作者:
GBKEE
時間:
2012-10-16 21:11
回復
1#
maiko
Option Explicit
Sub OpenPath()
Dim fd As FileDialog, FileName As String, FileFolder As String
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
With fd
.AllowMultiSelect = False
Do
If .Show = True Then FileFolder = .SelectedItems(1)
Loop Until .SelectedItems.Count <> 0
End With
Set fd = Nothing
End Sub
複製代碼
作者:
Hsieh
時間:
2012-10-16 22:52
回復
1#
maiko
為了能判斷是否有選擇資料夾,最好指定InitialFileName
因為對話方塊是要取得資料夾目錄,所以當你未選取資料夾(資料夾名稱欄位為空白)按下確定,SelectedItems.Count 仍然會是1
Sub OpenPath()
Dim fd As FileDialog, FileName As String, FileFolder As String, StartFolder$
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
StartFolder = "C:\Windows" '自行修改對話方塊的預設資料夾目錄
With fd
.AllowMultiSelect = False
Do
'.InitialFileName = StartFolder '預設對話方塊開啟在C:\Windows\資料夾名稱顯示Windows
.InitialFileName = StartFolder & IIf(Right(StartFolder, 1) = "\", "", "\") '預設對話方塊資料夾名稱為空白
.Show
If .SelectedItems.Count = 1 Then FileFolder = .SelectedItems(1)
Loop Until FileFolder <> StartFolder And .SelectedItems.Count = 1
End With
Set fd = Nothing
End Sub
複製代碼
作者:
maiko
時間:
2012-10-17 06:55
謝謝兩位指教,兩位的代碼一樣可以用到,只是Hsieh大的代碼如果不選的話,會一直都在InitialFileName的位置,每次都要重新選取新位置,不是根據之前的記錄來選取,所以比較麻煩一點,不過還是可以用的啦!謝謝!
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)