sigil
sigil

Reputation: 9546

FileOpenDialog requires 2 "OK" clicks to return value?

I'm using VBA to create a FileOpenDialog object so that the user can select a directory. Here's my test code:

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

But the dialog box created by this function requires that the user click OK twice to select whatever folder they've clicked on. Is there any way to make it so they only have to click OK once?

Upvotes: 0

Views: 826

Answers (1)

GSerg
GSerg

Reputation: 78175

You call .Show() twice. So the dialog shows twice. Each time you only have to click OK once.

Call .Show only once and save the returned value to a variable to test later.

Upvotes: 4

Related Questions