Results 1 to 2 of 2

Thread: Disable the 'x' close button on a VBA UserForm

  1. #1

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

    Arrow Disable the 'x' close button on a VBA UserForm

    This code will disable the 'x' close button on a Office VBA UserForm.

    Demo with Excel 2003.

    Note: you need to give the user a method of exiting the form or they will be forced to close the app if you also show the user form modally (default).
    Also, you need to Enable Macros for the attached example to run showing the userform at startup.

    Add a new UserForm to an Office app by way of the VBA IDE (press Alt+F11). Then add a command button to give the user a way to dismiss the userform.





    VB Code:
    1. Option Explicit
    2. 'Written By VB/Office Guru™
    3. 'Add a Command Button so you can close the userform
    4. Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, _
    5. ByVal wFlags As Long) As Long
    6.  
    7. Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
    8.  
    9. Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, _
    10. ByVal lpWindowName As String) As Long
    11.  
    12. Private Const MF_BYPOSITION = &H400&
    13.  
    14. Private Sub CommandButton1_Click()
    15.     Unload Me
    16. End Sub
    17.  
    18. Private Sub UserForm_Initialize()
    19.     Dim lHwnd As Long
    20.     lHwnd = FindWindow("ThunderDFrame", "UserForm1") 'Change to match your userforms caption
    21.     Do While lHwnd = 0
    22.         lHwnd = FindWindow("ThunderDFrame", "UserForm1") 'Change to match your userforms caption
    23.         DoEvents
    24.     Loop
    25.     RemoveMenu GetSystemMenu(lHwnd, 0), 6, MF_BYPOSITION 'When using by position, 6 represents the 7th menu item (including separators)
    26. End Sub
    Attached Images Attached Images  
    Attached Files Attached Files
    Last edited by RobDog888; Jan 26th, 2006 at 07:28 PM.
    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

  2. #2
    New Member
    Join Date
    Jan 2016
    Posts
    1

    Re: Disable the 'x' close button on a VBA UserForm

    Quote Originally Posted by RobDog888 View Post
    This code will disable the 'x' close button on a Office VBA UserForm.

    Demo with Excel 2003.

    Note: you need to give the user a method of exiting the form or they will be forced to close the app if you also show the user form modally (default).
    Also, you need to Enable Macros for the attached example to run this program it's the wealthy affiliate showing the userform at startup.

    Add a new UserForm to an Office app by way of the VBA IDE (press Alt+F11). Then add a command button to give the user a way to dismiss the userform.





    VB Code:
    1. Option Explicit
    2. 'Written By VB/Office Guru™
    3. 'Add a Command Button so you can close the userform
    4. Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, _
    5. ByVal wFlags As Long) As Long
    6.  
    7. Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
    8.  
    9. Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, _
    10. ByVal lpWindowName As String) As Long
    11.  
    12. Private Const MF_BYPOSITION = &H400&
    13.  
    14. Private Sub CommandButton1_Click()
    15.     Unload Me
    16. End Sub
    17.  
    18. Private Sub UserForm_Initialize()
    19.     Dim lHwnd As Long
    20.     lHwnd = FindWindow("ThunderDFrame", "UserForm1") 'Change to match your userforms caption
    21.     Do While lHwnd = 0
    22.         lHwnd = FindWindow("ThunderDFrame", "UserForm1") 'Change to match your userforms caption
    23.         DoEvents
    24.     Loop
    25.     RemoveMenu GetSystemMenu(lHwnd, 0), 6, MF_BYPOSITION 'When using by position, 6 represents the 7th menu item (including separators)
    26. End Sub
    RobDog888 will this work on the latest version of excel? I need to be able to do this.
    Last edited by Fuller; Jun 4th, 2016 at 08:30 AM.

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