Microsoft KB Archive/213502

From BetaArchive Wiki
Knowledge Base


XL2000: How to Programmatically Disable Microsoft Excel Control Menu Commands

Article ID: 213502

Article Last Modified on 11/23/2006



APPLIES TO

  • Microsoft Excel 2000 Standard Edition



This article was previously published under Q213502

For a Microsoft Excel 97 and earlier version of this article, see 107689.


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.

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 the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

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)
                


Additional query words: call register remove api XL2000

Keywords: kbhowto kbinfo kbprogramming KB213502