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:
Option Explicit
'Written By VB/Office Guru™
'Add a Command Button so you can close the userform
Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, _
ByVal wFlags As Long) As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Const MF_BYPOSITION = &H400&
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub UserForm_Initialize()
Dim lHwnd As Long
lHwnd = FindWindow("ThunderDFrame", "UserForm1") 'Change to match your userforms caption
Do While lHwnd = 0
lHwnd = FindWindow("ThunderDFrame", "UserForm1") 'Change to match your userforms caption
DoEvents
Loop
RemoveMenu GetSystemMenu(lHwnd, 0), 6, MF_BYPOSITION 'When using by position, 6 represents the 7th menu item (including separators)
End Sub
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.
Re: Disable the 'x' close button on a VBA UserForm
Originally Posted by RobDog888
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:
Option Explicit
'Written By VB/Office Guru™
'Add a Command Button so you can close the userform
Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, _
ByVal wFlags As Long) As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Const MF_BYPOSITION = &H400&
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub UserForm_Initialize()
Dim lHwnd As Long
lHwnd = FindWindow("ThunderDFrame", "UserForm1") 'Change to match your userforms caption
Do While lHwnd = 0
lHwnd = FindWindow("ThunderDFrame", "UserForm1") 'Change to match your userforms caption
DoEvents
Loop
RemoveMenu GetSystemMenu(lHwnd, 0), 6, MF_BYPOSITION 'When using by position, 6 represents the 7th menu item (including separators)
End Sub
RobDog888 will this work on the latest version of excel? I need to be able to do this.