PDA

View Full Version : Check if "Browse For Folder" window already open



gatonegro75
12-17-2008, 09:10 AM
I've been using Ken Puls? ?Browse For Folder? function. I'm using it in Access rather than Excel - it works very nicely.

(I tried to post the link but I don't have enough posts under my belt)

But I would like to find out how to check if the folder browser window is already open.

I have the code being executed from a form command button. But if a user clicks on this button multiple times, multiple windows will open. I want to check to see if the window is already open from within the function and if exit the function.

Kenneth Hobs
12-17-2008, 09:31 AM
If you are into API's then add this one.
Declare Function FindWindow _
Lib "user32" _
Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

As you know, API's are put at the top of modules before Subs and Functions.

If your routine that calls the other API is called Test then the commandbutton1 would go something like:
Private Sub CommandButton1_Click()
Dim rc As Long
rc = FindWindow(vbNullString, "Browse for Folder")
If rc Then
AppActivate "Browse for Folder", True
Else: Test
End If
End Sub

Application.FileDialog is what I typically use for browsing folders but it is not supported in 2003- versions. e.g.
Function GetFolder(Optional sTitle As String = "Select Folder", _
Optional sInitialFilename As String)
Dim myFolder As String
With Application.FileDialog(msoFileDialogFolderPicker)
If sInitialFilename = "" Then sInitialFilename = ThisWorkbook.path
.initialFilename = sInitialFilename
.Title = "Greetings"
If .Show = -1 Then
GetFolder = .SelectedItems(1)
If Right(GetFolder, 1) <> "\" Then
GetFolder = GetFolder & "\"
End If
Else: GetFolder = ""
End If
End With
End Function

Benzadeus
12-17-2008, 09:56 AM
Assuming the command button name is CommandButton1:

Dim bolAlreadyOpen As Boolean
Private Sub CommandButton1_Click()
If bolAlreadyOpen = False Then
bolAlreadyOpen = True
' Ken Pul's Code begins here
Dim result As String
result = BrowseForFolder
Select Case result
Case Is = False
result = "an invalid folder!"
Case Else
'don't change anything
End Select
MsgBox "You selected " & result, _
vbOKOnly + vbInformation
' Ken Pul's code ends here
bolAlreadyOpen = False
End If
End Sub


Alternative: You can put

Private Sub CommandButton1_Click()

UserForm1.Visible = True

'Code here

UserForm1.Visible = False

End Sub




Alternative: You can put
*Worst choice because window lost focus.

Private Sub CommandButton1_Click()

UserForm1.Enabled = False

'Code here

UserForm1.Enabled = True
End Sub

gatonegro75
12-17-2008, 10:42 AM
Thanks Kenneth - that worked a treat. Obviously the special relationshiop betwenn the USA and the UK is still going strong!

Tom