Public Const MF_BYPOSITION = &H400&
Public Const MF_GRAYED = &H1&
Public Const MF_REMOVE = &H1000
Public Declare Function GetActiveWindow Lib "user32" () As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function GetSystemMenu Lib "user32.dll" (ByVal hwnd As Long, ByVal bRevert _
As Long) As Long
Public Declare Function GetMenuItemCount Lib "user32.dll" (ByVal hMenu As Long) As Long
Public Declare Function EnableMenuItem Lib "user32" (ByVal hMenu As Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long
Public Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, _
ByVal nPosition As Long, ByVal wFlags As Long) As Long
Sub DisableSysMenuClose()
Dim hwnd As Long
Dim hSysMenu As Long
Dim retVal As Long
Dim Count As Long
Dim wCaption As String
wCaption = String$(256, 0)
hwnd = GetActiveWindow
retVal = GetWindowText(hwnd, wCaption, 255)
wCaption = Left$(wCaption, retVal)
If InStr(1, wCaption, "Microsoft Excel", vbTextCompare) = 0 Then
Exit Sub
End If
hSysMenu = GetSystemMenu(hwnd, 0)
Count = GetMenuItemCount(hSysMenu)
' The following call does not seem to work; see notes
' Call EnableMenuItem(hSysMenu, Count - 1, MF_GRAYED Or MF_BYPOSITION)
' Instead, remove Close menu item + separator bar
Call RemoveMenu(hSysMenu, Count - 1, MF_REMOVE Or MF_BYPOSITION)
Call RemoveMenu(hSysMenu, Count - 2, MF_REMOVE Or MF_BYPOSITION)
End Sub
Sub EnableSysMenuClose()
Dim hwnd As Long
Dim hSysMenu As Long
Dim retVal As Long
Dim wCaption As String
wCaption = String$(256, 0)
hwnd = GetActiveWindow
retVal = GetWindowText(hwnd, wCaption, 255)
wCaption = Left$(wCaption, retVal)
If InStr(1, wCaption, "Microsoft Excel", vbTextCompare) <> 0 Then
hSysMenu = GetSystemMenu(hwnd, True)
End If
End Sub
Sub DisableExitKey()
Application.OnKey key:="%{F4}", procedure:=""
End Sub
Sub EnableExitKey()
Application.OnKey key:="%{F4}"
End Sub