Results 1 to 6 of 6

Thread: [RESOLVED] How to disable userform close button

  1. #1

    Thread Starter
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Resolved [RESOLVED] How to disable userform close button

    When I create userforms within Word 2003, the default close button (red cross) appears on the top right corner of each form.

    My forms are set up so that the user has to:

    Fill in the form and click cmdOK to complete the document.
    Or
    Press cmdCancel, in which case Word exits and no changes are applied.

    Does anybody know a means to disable this button or, if it can not be disabled easily, execute my cmdCancel code when it is pressed?

    Thanks in advance
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

  2. #2
    Lively Member
    Join Date
    Jun 2005
    Posts
    112

    Re: How to disable userform close button

    Add this code to your form.
    VB Code:
    1. Private Sub UserForm_Terminate()
    2.     Call cmdCancel_Click
    3. End Sub

  3. #3
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: How to disable userform close button

    I prefer to use the _QueryClose event of the form.

    Using _Terminate means that the cmdCancel_Click proedure is called even when you close the form programatically, unless you trap for that.

    VB Code:
    1. Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    2.     If CloseMode = vbFormControlMenu Then
    3.         cmdCancel_Click
    4.         Cancel = True
    5.     End If
    6. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  4. #4

    Thread Starter
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Thumbs up Re: How to disable userform close button

    Thanks for the response mikeyc1204 and DKenny.

    I tried both methods, but had a couple of problems. As you pointed out DKenny, using _Terminate caused an error when the form was closed programatically, i.e. using cmdCancel, and I don't know how to "trap" for that (excuse the ignorance ).

    The _QueryClose event sort of worked - the document closed and no changes were saved, although the form itself remained open and I had to ctrl+alt+del to close it. To resolve this I removed Cancel = True and it works perfectly.

    Thanks again for the help.
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] How to disable userform close button

    Perhaps actually disabling the 'x' would work better for you.

    http://www.vbforums.com/showthread.php?t=363931
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Re: [RESOLVED] How to disable userform close button

    Thanks RobDog888.
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

Posting Permissions

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



Click Here to Expand Forum to Full Width