Consulting

Results 1 to 4 of 4

Thread: Check if "Browse For Folder" window already open

  1. #1

    Check if "Browse For Folder" window already open

    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.

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    If you are into API's then add this one.
    [VBA]Declare Function FindWindow _
    Lib "user32" _
    Alias "FindWindowA" _
    (ByVal lpClassName As String, _
    ByVal lpWindowName As String) As Long[/VBA]

    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:
    [VBA]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[/VBA]

    Application.FileDialog is what I typically use for browsing folders but it is not supported in 2003- versions. e.g.
    [VBA]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
    [/VBA]

  3. #3
    VBAX Tutor Benzadeus's Avatar
    Joined
    Dec 2008
    Location
    Belo Horizonte, Brazil
    Posts
    271
    Location
    Assuming the command button name is CommandButton1:
    [VBA]
    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
    [/VBA]

    Alternative: You can put
    [VBA]
    Private Sub CommandButton1_Click()

    UserForm1.Visible = True

    'Code here

    UserForm1.Visible = False

    End Sub
    [/VBA]



    Alternative: You can put
    *Worst choice because window lost focus.
    [VBA]
    Private Sub CommandButton1_Click()

    UserForm1.Enabled = False

    'Code here

    UserForm1.Enabled = True
    End Sub
    [/VBA]

  4. #4
    Thanks Kenneth - that worked a treat. Obviously the special relationshiop betwenn the USA and the UK is still going strong!

    Tom

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •