Results 1 to 6 of 6
  1. #1
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    856
    Thanks
    0
    Thanked 9 Times in 8 Posts

    Modeless Forms (Word 12)

    Is there a way to determine whether a user form is currently displayed? I am displaying a user form as Modeless. A button on the Ribbon displays it, and I'd like the same button to hide the form it it's currently displayed.

    I suppose I could use a public Boolean variable, but that has its drawbacks. I've had OK luck with a function that looks at frmCustom.ActiveControl. That raises an error if the user form is not currently displayed, so the error triggers the function to return a False; otherwise True. I'm wondering if there's a better way.

    Thanks.

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 15 Times in 15 Posts

    Re: Modeless Forms (Word 12)

    The UserForms collection contains the currently loaded forms. If no form is loaded, UserForms.Count equals 0.
    Warning: a userform can be hidden but still loaded - you as programmer should know whether this is relevant in your situation.
    You can loop through the items of the UserForms collection to inspect their Name and Visible properties.

  4. #3
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    856
    Thanks
    0
    Thanked 9 Times in 8 Posts

    Re: Modeless Forms (Word 12)

    Thank you. I didn't have very good luck with that though. The code below runs with the results which I've added as comments. .Name and .Visible don't appear to be valid properties.

    Sub testUserForms()
    Load frmMarkDT
    With frmMarkDT
    MsgBox .Caption ' Displays caption as expected
    End With
    Dim uf As UserForm
    MsgBox UserForms.Count ' count = 1 as expected
    For Each uf In UserForms
    MsgBox uf.Caption ' MsgBox is empty
    Next
    End Sub

    This is the code which seems to work, although it may be a bit unconventional. Do you think there's any potential problem with it? (I could make it more generic by pass the form to it.)

    Function MarkDTDisplayed()
    On Error GoTo ErrorTrap
    Dim x
    With frmMarkDT
    x = .ActiveControl
    End With
    MarkDTDisplayed = True
    Exit Function
    ErrorTrap:
    MarkDTDisplayed = False
    End Function

    Regards,
    Richard

  5. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 15 Times in 15 Posts

    Re: Modeless Forms (Word 12)

    The first macro will work (also with .Name and .Visible) if you declare uf as a Variant instead of as a UserForm:

    Dim uf As Variant

  6. #5
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    856
    Thanks
    0
    Thanked 9 Times in 8 Posts

    Re: Modeless Forms (Word 12)

    So it does. Thank you... that's good to know.

  7. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 15 Times in 15 Posts

    Re: Modeless Forms (Word 12)

    UserForms are strange creatures - Name and Visible are not valid properties of the UserForm object, but they are valid for specific instances of a userform... <img src=/S/crazy.gif border=0 alt=crazy width=15 height=15>

Posting Permissions

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