In some cases, you do not want the user to exit via the 'Close'-button. Try this:

Randy Birch's Example
RemoveMenu: Killing the Form's Close Menu and 'X' Button
(deactivate 'X'(close) in the systemmenu)

The result looks like this:

Adapted for VBA:


Option Explicit

Private Const MF_BYPOSITION = &H400
Private Const MF_REMOVE = &H1000

Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Long) As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, _
   ByVal bRevert As Long) As Long
Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, _
   ByVal nPosition As Long, ByVal wFlags As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
   (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Sub UserForm_Initialize()
   Dim hMenu As Long
   Dim menuItemCount As Long
   Dim hWnd_UF As Long
   
   hWnd_UF = FindWindow("ThunderDFrame", Me.Caption)
   
   'Obtain the handle to the form's system menu
   hMenu = GetSystemMenu(hWnd_UF, 0)
   
   If hMenu Then
      'Obtain the number of items in the menu
      menuItemCount = GetMenuItemCount(hMenu)
      
      'Remove the system menu Close menu item.
      'The menu item is 0-based, so the last
      'item on the menu is menuItemCount - 1
      RemoveMenu hMenu, menuItemCount - 1, MF_REMOVE Or MF_BYPOSITION
      
      'Remove the system menu separator line
      RemoveMenu hMenu, menuItemCount - 2, MF_REMOVE Or MF_BYPOSITION
      
      'Force a redraw of the menu. This
      'refreshes the titlebar, dimming the X
      DrawMenuBar hWnd_UF
   End If
End Sub


In order to test the example, copy the entire code to a new UserForm.

Go to top