XL97: Sample Macros to Control Menus and Submenus

Last reviewed: February 27, 1998
Article ID: Q159619
The information in this article applies to:
  • Microsoft Excel 97 for Windows

SUMMARY

The process of programmatically changing menus in Microsoft Excel 97 is different than earlier versions of Microsoft Excel. The biggest change in Microsoft Excel 97 is that toolbars, menu bars, and shortcut menus are considered one type of object (called a command bar). Most Visual Basic for Applications macros that you create in earlier versions of Microsoft Excel that customize menus and toolbars work in Microsoft Excel 97. However, some macros may fail. In this case, modify the macro code to work with the new object type.

This article provides several examples that illustrate how to customize menu bars, menus, and toolbars in Microsoft Excel 97.

NOTE: In many of the example macros in this article, the ID number for a particular control is used as an argument for the Add method. You must know the ID number if you want to restore built-in menus that you deleted.

For additional information, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: XL97: List of ID Numbers for Built-In Command Bar Controls
   TITLE     : Q159466

MORE INFORMATION

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.

Deleting an Entire Menu Bar

The following macro disables the Worksheet menu bar. After you run the following macro, the Worksheet menu bar is not displayed when a worksheet is active.

   Sub Disable_Menu_Bar()

       CommandBars("Worksheet Menu Bar").Enabled = False

   End Sub

The following macro re-enables the Worksheet menu bar so that is displayed when a worksheet is active.

   Sub Enable_Menu_Bar()

       CommandBars("Worksheet Menu Bar").Enabled = True

   End Sub

Deleting and Restoring a Menu on a Menu Bar

The following macro deletes the Help menu from the Worksheet menu bar.

   Sub Delete_Help_Menu()

       CommandBars("Worksheet Menu Bar").Controls("Help").Delete

   End Sub

The following macro restores the Help menu to the Worksheet menu bar.

   Sub Restore_Help_Menu()

       set x = CommandBars("Worksheet Menu Bar")
       x.Reset

   End Sub

NOTE: This macro resets the entire Worksheet menu bar back to its default settings. When you run this macro, all customizations that you made to the Worksheet menu bar will be lost.

Deleting and Restoring a Menu Command on a Menu

The following macro deletes the Contents And Index menu command on the Help menu.

   Sub Delete_Menu_Item()

       Set x = CommandBars("Worksheet Menu Bar").Controls("Help")
       x.Controls("Contents and Index").Delete

   End Sub

The following macro restores the Contents And Index menu command on the Help menu.

   Sub Restore_Menu_Item()

       Set x = CommandBars("Worksheet Menu Bar").Controls("Help")
       x.Controls.Add Type:=msoControlButton, Id:=983, before:=2

   End Sub

Deleting and Restoring a Submenu on a Menu

The following macro deletes the "Microsoft on the Web" submenu on the Help menu.

   Sub Delete_Submenu()

       Set x = CommandBars("Worksheet Menu Bar").Controls("Help")
       x.Controls("Microsoft on the Web").Delete

   End Sub

The following macro restores the "Microsoft on the Web " submenu on the Help menu.

   Sub Restore_Submenu()

       Set x = CommandBars("Worksheet Menu Bar").Controls("Help")
       x.Controls.Add Type:=msoControlPopup, Id:=30222, before:=4
       x.Reset

   End Sub

Deleting and Restoring a Menu Command on a Submenu

The following macro deletes the Protect Sheet menu command on the Protection submenu (on the Tools menu).

   Sub Delete_Item_on_Submenu()

       Set x = CommandBars("Tools").Controls("Protection")
       x.Controls("Protect Sheet...").Delete

   End Sub

The following macro restores the Protect Sheet menu command on the Protection submenu (on the Tools menu).

   Sub Restore_Item_on_Submenu()

       Set x = CommandBars("Tools").Controls("Protection")
       x.Controls.Add Type:=msoControlButton, Id:=893, before:=1

   End Sub

Deleting and Restoring a Menu on a Toolbar

The following macro deletes the Draw menu on the Drawing toolbar.

   Sub Delete_Menu_on_Toolbar()

       CommandBars("Drawing").Controls("Draw").Delete

   End Sub

The following macro restores the Draw menu on the Drawing toolbar.

   Sub Restore_Menu_on_Toolbar()

       Set x = CommandBars("Drawing")
       x.Controls.Add Type:=msoControlPopup, Id:=30013, before:=1
       x.Reset

   End Sub

Deleting and Restoring a Menu Item on a Shortcut Menu

The following macro deletes the Insert Comment menu command on the worksheet cell shortcut menu.

   Sub Delete_Shortcut_menu_item()

       CommandBars("Cell").Controls("Insert Comment").Delete

   End Sub

The following macro restores the Insert Comment menu command on the worksheet cell shortcut menu and restores the separator line that the previous macro deleted.

   Sub Restore_Shortcut_menu_item()

       Set x = CommandBars("cell")
       x.Controls.Add Type:=msoControlButton, Id:=2031, before:=8
       Application.ShortcutMenus(xlWorksheetCell).MenuItems.Add _
           Caption:="-", before:=9

   End Sub

REFERENCES

For more information about programmatically customizing command bars, click Microsoft Visual Basic Help on the Help menu in the Visual Basic Editor, type "Customizing menus," click Search, and then click "Overview of Command Bars" to go to the topic.


Additional query words: XL97 8.0 8.00 commandbar menubar
Keywords : kbcode kbprg kbualink97 xlvbahowto xlvbainfo xlui
Version : WINDOWS: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: February 27, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.