我正在使用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一次?
答案 0 :(得分:4)
您拨打.Show()
两次。对话框显示两次。每次只需单击“确定”一次。
仅调用.Show
一次并将返回的值保存到变量中以便稍后进行测试。