Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

XL2000: How to Programmatically Disable Microsoft Excel Control Menu Commands


View products that this article applies to.

This article was previously published under Q213502
For a Microsoft Excel 97 and earlier version of this article, see 107689 (http://support.microsoft.com/kb/107689/EN-US/ ) .

↑ Back to the top


Summary

This article describes how to programmatically delete the commands on the Microsoft Windows Control menu (right-click the Microsoft Excel title bar) and disable the Windows control buttons in the upper-right corner of the Excel window.

↑ Back to the top


More information

Microsoft provides programming examples 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. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs.
If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:

Microsoft Certified Partners - https://partner.microsoft.com/global/30000104

Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS Excel does not have the built-in functionality to modify the Windows Control menu commands. However, you can use the Declare statement in a Microsoft Visual Basic procedure to call Windows functions to disable and restore Control menu items.

The following sample Visual Basic macro, Disable_Control, disables the entire Windows Control menu in Excel. The macro disables the Control menu for the current session of Excel (when you restart Excel, the Control menu is reset). The macro RestoreSystemMenu restores the Control menu.
  1. Start Excel, and then press ALT+F11 to start the Visual Basic Editor.
  2. On the Insert menu, click Module.
  3. In the module sheet, type the following code:
    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _ 
          (ByVal lpClassName As String, ByVal lpWindowName As String) As Long 
    
    Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, _ 
          ByVal bRevert As Integer) As Integer 
    
    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, hwnd As Long 
   hwnd = FindWindow("XLMain", Application.Caption) 
   For X = 1 To 9 
      'Delete the first menu command and loop until 
      'all commands are deleted 
      Call DeleteMenu(GetSystemMenu(hwnd, False), 0, 1024) 
   Next X 
End Sub 
					
The following procedure restores the Control menu, and to run this procedure, the Declare statements above must be in the module:
Sub RestoreSystemMenu() 
   Dim hwnd As Long 
   'get the window handle of the Excel application 
   hwnd = FindWindow("xlMain", Application.Caption) 
   'restore system menu to original state 
   hMenu% = GetSystemMenu(hwnd, 1) 
End Sub 
					
The commands on the Control menu are numbered, starting at zero. The default Control menu items are as follows: Restore is item 0, Move is item 1, Size is item 2, and so forth. 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 Disable_Control macro, delete the Maximize (item 4) and Minimize (item 3) commands and disable the Maximize and Minimize buttons:
   Call DeleteMenu(GetSystemMenu(FindWindow("xlMain", _ 
         Application.Caption), False), 4, 1024) 
   Call DeleteMenu(GetSystemMenu(FindWindow("xlMain", _ 
         Application.Caption), False), 3, 1024)
				

↑ Back to the top


Keywords: KB213502, kbprogramming, kbinfo, kbhowto

↑ Back to the top

Article Info
Article ID : 213502
Revision : 9
Created on : 11/23/2006
Published on : 11/23/2006
Exists online : False
Views : 291