rmikesmith
Technical User
Hello All,
I am trying to disable Excel's system menu 'Close' command using the EnableMenuItem API function, in VBA. I cannot get this to work, although the similar RemoveMenuItem function works perfectly. Here is the procedure I've written:
' The following call does not seem to work
' While these next 2 calls perform as expected (removing
' the Close menu item and the separator line above it)
Some additional info: The call to EnableMenuItem worked when I ran the procedure from within the VBA Editor. I have single-stepped through the code when running from the main Excel window and it appears to be registering this (i.e. the code to read the window caption is returning "Microsoft Excel"
but after returning from the EnableMenuItem function call, there is no apparent change to the system menu. I have also tried using the MF_DISABLED constant with and without MF_GRAYED, without success.
Any help is greatly appreciated.
Mike
I am trying to disable Excel's system menu 'Close' command using the EnableMenuItem API function, in VBA. I cannot get this to work, although the similar RemoveMenuItem function works perfectly. Here is the procedure I've written:
Code:
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
Code:
Call EnableMenuItem(hSysMenu, Count - 1, MF_GRAYED Or MF_BYPOSITION)
' the Close menu item and the separator line above it)
Code:
Call RemoveMenu(hSysMenu, Count - 1, MF_REMOVE Or MF_BYPOSITION)
Call RemoveMenu(hSysMenu, Count - 2, MF_REMOVE Or MF_BYPOSITION)
End Sub
Some additional info: The call to EnableMenuItem worked when I ran the procedure from within the VBA Editor. I have single-stepped through the code when running from the main Excel window and it appears to be registering this (i.e. the code to read the window caption is returning "Microsoft Excel"
Any help is greatly appreciated.
Mike