XL: Disabling Microsoft Excel Control Menu Commands

Last reviewed: September 2, 1997
Article ID: Q107689
The information in this article applies to:
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for Windows 95, version 7.0
  • Microsoft Excel 97 for Windows

SUMMARY

You can use the Microsoft Windows Dynamic Link Libraries (DLLs) to disable the commands in the Microsoft Excel Control menu. For example, you can use these tools to delete the Minimize and Maximize buttons.

MORE INFORMATION

Microsoft Excel does not have the built-in functionality to modify the Control menu commands. However, you can use the Declare statement in a Visual Basic procedure to call Microsoft Windows functions to disable and restore Control menu items.

The following example Visual Basic procedure, Disable_Control, disables the entire Control menu in Microsoft Excel. The macro disables the Control menu for the current session of Microsoft Excel (when you restart Microsoft Excel the Control menu will be reset). The procedure RestoreSystemMenu restores the Control menu.

Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

Microsoft Excel version 97 and 7.0

Declare Function GetActiveWindow Lib "User32" () As Integer

'Enter the following Declare statement as a single line
Declare Function GetSystemMenu Lib "User32" (ByVal hWnd As Integer, ByVal
bRevert As Integer) As Integer

'Enter the following Declare statement as a single line
Declare Function DeleteMenu Lib "User32" (ByVal hMenu As Integer, ByVal
nPosition As Integer, ByVal wFlags As Integer) As Integer

'The following procedure disables the Control menu
Sub Disable_Control()
   Dim X As Integer
   For X = 1 To 9
      'Delete the first menu command and loop until
      'all commands are deleted
      Call DeleteMenu(GetSystemMenu(GetActiveWindow, False), 0, 1024)
   Next X
End Sub

'The following procedure restores the Control menu
'Note that to run this procedure, the Declare statements above
'must be in the module
Sub RestoreSystemMenu()
   'get the window handle of the Excel application
   hWnd = GetActiveWindow()
   'restore system menu to original state
   hMenu% = GetSystemMenu(hWnd, 1)
End Sub

Microsoft Excel version 5.x

'The following procedure disables the Control menu
Declare Function GetActiveWindow Lib "User" () As Integer

'Enter the following Declare statement as a single line
Declare Function GetSystemMenu Lib "User" (ByVal hWnd As Integer, ByVal
bRevert As Integer) As Integer

'Enter the following Declare statement as a single line
Declare Function DeleteMenu Lib "User" (ByVal hMenu As Integer, ByVal
nPosition As Integer, ByVal wFlags As Integer) As Integer

Sub Disable_Control()
   Dim X as Integer
   For X = 1 to 9
      'Delete the first menu command and loop until
      'all commands are deleted
      Call DeleteMenu(GetSystemMenu(GetActiveWindow, False),0,1024)
   Next X
End Sub

'The following procedure restores the Control menu
'Note that to run this procedure, the Declare statements above
'must be in the module
Sub RestoreSystemMenu()
   'get the window handle of the Excel application
   hWnd = GetActiveWindow()
   'restore system menu to original state
   hMenu% = GetSystemMenu(hWnd, 1)
End Sub


Notes

The commands on the control menu are numbered starting at zero. The default control menu items are as follows: Control Menu Restore is item 0, Move is item 1, Size is item 2, and so on. Even if items are deleted, the first item always starts at zero.

To delete individual items from the control menu without deleting the entire menu, you can specify the menu command to delete. For example the following two lines of code when used in place of the For Next loop in the procedure Disable_Control above, will delete the Maximize (item 4) and Minimize (item 3) commands and disable the Maximize and Minimize buttons.

   Call DeleteMenu(GetSystemMenu(GetActiveWindow, False),4,1024)
   Call DeleteMenu(GetSystemMenu(GetActiveWindow, False),3,1024)


Additional reference words: 5.00 5.00c 7.00 8.00 97 call register remove
api
Keywords : kbprg PgmHowTo PgmOthr kbcode
Version : 5.00 5.00c 7.00 97
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 2, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.