FileOpenDialog需要2次“OK”点击才能返回值?

时间:2011-11-02 21:07:00

标签: vba

我正在使用VBA创建一个FileOpenDialog对象,以便用户可以选择一个目录。这是我的测试代码:

Function GetFolder(InitDir As String) As String
Dim fldr As FileDialog
Dim sItem As String
sItem = InitDir
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Please select a folder, then press OK to continue"
.AllowMultiSelect = False
If Right(sItem, 1) <> "\" Then
sItem = sItem & "\"
End If
.InitialFileName = sItem
If .Show = 0 Then
    GetFolder = ""
    Exit Function
End If
If .Show <> -1 Then
sItem = InitDir
Else
sItem = .SelectedItems(1)
End If
End With
GetFolder = sItem
Set fldr = Nothing
End Function

sub test()

dim selectedDir as variant

selectedDir=getFolder("c:")
msgbox selectedDir

end sub

但是,此功能创建的对话框要求用户单击“确定”两次以选择他们单击的任何文件夹。有没有办法让它们只需要点击OK一次?

1 个答案:

答案 0 :(得分:4)

您拨打.Show()两次。对话框显示两次。每次只需单击“确定”一次。

仅调用.Show一次并将返回的值保存到变量中以便稍后进行测试。