Board logo

標題: [發問] 此段程式中如何加一個判斷? [打印本頁]

作者: maiko    時間: 2012-10-16 17:23     標題: 此段程式中如何加一個判斷?

  1. Sub OpenPath()

  2. Dim fd As FileDialog, FileName As String, FileFolder As String

  3. Set fd = Application.FileDialog(msoFileDialogFolderPicker)

  4. With fd
  5.     .AllowMultiSelect = False
  6.     If .Show = -1 Then
  7.         FileFolder = .SelectedItems(1)
  8.     Else
  9.         End
  10.     End If
  11. End With

  12. Set fd = Nothing

  13. End Sub
複製代碼
如何加一個判斷如果沒有選取時就不能離開FileDialog?Thanks.
作者: GBKEE    時間: 2012-10-16 21:11

回復 1# maiko
  1. Option Explicit
  2. Sub OpenPath()
  3.     Dim fd As FileDialog, FileName As String, FileFolder As String
  4.     Set fd = Application.FileDialog(msoFileDialogFolderPicker)
  5.     With fd
  6.         .AllowMultiSelect = False
  7.         Do
  8.             If .Show = True Then FileFolder = .SelectedItems(1)
  9.         Loop Until .SelectedItems.Count <> 0
  10.     End With
  11.     Set fd = Nothing
  12. End Sub
複製代碼

作者: Hsieh    時間: 2012-10-16 22:52

回復 1# maiko
為了能判斷是否有選擇資料夾,最好指定InitialFileName
因為對話方塊是要取得資料夾目錄,所以當你未選取資料夾(資料夾名稱欄位為空白)按下確定,SelectedItems.Count 仍然會是1
  1. Sub OpenPath()
  2.     Dim fd As FileDialog, FileName As String, FileFolder As String, StartFolder$
  3.     Set fd = Application.FileDialog(msoFileDialogFolderPicker)
  4.     StartFolder = "C:\Windows"  '自行修改對話方塊的預設資料夾目錄
  5.     With fd
  6.         .AllowMultiSelect = False
  7.         Do
  8.         '.InitialFileName = StartFolder '預設對話方塊開啟在C:\Windows\資料夾名稱顯示Windows
  9.         .InitialFileName = StartFolder & IIf(Right(StartFolder, 1) = "\", "", "\") '預設對話方塊資料夾名稱為空白
  10.         .Show
  11.         If .SelectedItems.Count = 1 Then FileFolder = .SelectedItems(1)
  12.         Loop Until FileFolder <> StartFolder And .SelectedItems.Count = 1
  13.     End With
  14.     Set fd = Nothing
  15. End Sub
複製代碼

作者: maiko    時間: 2012-10-17 06:55

謝謝兩位指教,兩位的代碼一樣可以用到,只是Hsieh大的代碼如果不選的話,會一直都在InitialFileName的位置,每次都要重新選取新位置,不是根據之前的記錄來選取,所以比較麻煩一點,不過還是可以用的啦!謝謝!




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)