Disable the X for Closing a Userform

Just as the previous example disabled the X button of the Excel application, a slight modification disables the X button on a userform, forcing the user to use the Close button. When the form is initialized, the button is disabled. After the form is closed, the X button is reset to normal:

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _ (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, _

ByVal bRevert As Long) As Long Private Declare Function DeleteMenu Lib "user32" _ (ByVal hMenu As Long, ByVal nPosition As Long, _ ByVal wFlags As Long) As Long Private Const SC_CLOSE As Long = &HF060

Private Sub UserForm_Initialize() Dim hWndForm As Long Dim hMenu As Long hWndForm = FindWindow("ThunderDFrame", Me.Caption) 'XL2000 hMenu = GetSystemMenu(hWndForm, 0) DeleteMenu hMenu, SC_CLOSE, 0&

End Sub

The DeleteMenu macro in the UserForm_Initialize procedure causes the X in the corner of the userform to be grayed out as shown in Figure 22.6. This forces the client to use your programmed Close button.

Figure 22.6

Disable the X button on a userform,forcing users to use the Close button to shut down the form properly and rendering them unable to bypass any code attached to the Close button.

Figure 22.6

Disable the X button on a userform,forcing users to use the Close button to shut down the form properly and rendering them unable to bypass any code attached to the Close button.

0 0

Post a comment