Results 1 to 6 of 6
  1. #1
    New Lounger
    Join Date
    Aug 2002
    Location
    England
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to determine if a UserForm is open (2002 SP1)

    I want to write some code to check if a UserForm is open,
    but using the following doesn't work:

    If [formname].Visible = False Then
    [run code here]
    End If

    What's the best answer?

    Many thanks

    Mark Hartt-Palmer

  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How to determine if a UserForm is open (2002 SP1)

    Check the Forms collection.

  3. #3
    Lounger
    Join Date
    Jan 2001
    Location
    Kingwood, Texas, USA
    Posts
    49
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How to determine if a UserForm is open (2002 SP1)

    I would love to know the answer to this, as well as to the larger question of how to tell if you have multiple instances of Word (or of a Word form) running invisibly in the background. Checking to see if a particular form is visible doesn't work, because a form may not be visible because it is not loaded.

    For example:
    (first create UserForm1 with its ShowModal set to True, then run the following macro
    Sub ShowMyForm()
    UserForm1.Show

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: How to determine if a UserForm is open (2002 SP1)

    Howard's suggestion is cryptic, and the documention is poor, but in Word 2000, this will give the expected results:
    <pre>Sub DoesUFLive()
    Debug.Print UserFormLoaded("UserForm1")
    Load UserForm1
    Debug.Print UserFormLoaded("UserForm1")
    UserForm1.Show vbModeless
    Debug.Print UserFormLoaded("UserForm1")
    UserForm1.Hide
    Debug.Print UserFormLoaded("UserForm1")
    Unload UserForm1
    Debug.Print UserFormLoaded("UserForm1")
    Set UserForm1 = Nothing
    Debug.Print UserFormLoaded("UserForm1")
    End Sub

    Function UserFormLoaded(strUFName As String) As Boolean
    ' This is a case sensitive comparison
    If UserForms.Count > 0 Then
    Dim intCounter As Integer
    For intCounter = 1 To UserForms.Count
    If UserForms(intCounter - 1).Name = strUFName Then
    UserFormLoaded = True
    Exit Function
    End If
    Next
    End If
    UserFormLoaded = False
    End Function</pre>

    Results:
    <pre>False
    True
    True
    True
    False
    False</pre>

    Hope this helps.

  5. #5
    New Lounger
    Join Date
    Aug 2002
    Location
    England
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How to determine if a UserForm is open (2002 SP1)

    Many thanks for your replies - the code works a treat!

  6. #6
    Lounger
    Join Date
    Jan 2001
    Location
    Kingwood, Texas, USA
    Posts
    49
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How to determine if a UserForm is open (2002 SP1)

    Thank you for your help!

Posting Permissions

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