XL97: How to Place a Checkmark Next to a Menu Item

Last reviewed: March 13, 1998
Article ID: Q161926
The information in this article applies to:
  • Microsoft Excel 97 for Windows

SUMMARY

If you are working with menus programmatically, you may want to add a checkmark next to a particular menu item to show the item is active. For example, when you point to Filter on the Data menu and then click AutoFilter, a checkmark is placed next to AutoFilter to show that the AutoFilter is currently turned on.

This article provides sample macros for programmatically adding a checkmark next to a custom menu item using the new CommandBars object model.

NOTE: There is no way to programmatically add a checkmark next to a built-in menu item; you can add checkmarks only to custom items.

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.

  1. Save and close any open workbooks and then open a new workbook.

  2. Start the Visual Basic Editor (press ALT+F11).

  3. On the Insert menu, click Module.

  4. In the module, add the following code:

          Sub checked_menuitem()
    
              'add a new docked CommandBar
              Set mybar = CommandBars.Add(Name:="my command bar", _
                  Position:=msoBarTop)
              mybar.Visible = True
    
              'add a menu to the custom CommandBar
              Set mypopup = mybar.Controls.Add(Type:=msoControlPopup)
              mypopup.Caption = "my menu"
    
              'add a menu item to the menu just added to the CommandBar
              Set myitem = mypopup.Controls.Add(Type:=msoControlButton)
              myitem.Caption = "my menu item"
              myitem.OnAction = "check_item"
    
          End Sub
    
          Sub check_item()
    
              Set mypopup = CommandBars("my command bar").Controls("my menu")
    
              If mypopup.Controls("my menu item").State = msoButtonDown Then
    
                  'remove check next to menu item
                  mypopup.Controls("my menu item").State = msoButtonUp
                  MsgBox "menu item is now unchecked"
              Else
    
                  'add check next to menu item
                  mypopup.Controls("my menu item").State = msoButtonDown
                  MsgBox "menu item is now checked"
              End If
          End Sub
    
    

  5. Run the checked_menuitem macro.

This will create a docked CommandBar with a single menu called "my menu".

  1. Activate Microsoft Excel (press ALT+F11).

  2. Click the "my menu" drop-down and then click "my menu item".

A message box will display a message stating the menu is now checked and the "my menu item" will have a checkmark placed next to it.

  1. Repeat Step 7.

A message box will display a message stating the menu is now unchecked and the "my menu item" will not have a checkmark placed next to it.

Removing the Custom CommandBar

  1. On the Tools menu, click Customize.

  2. On the Toolbars tab, scroll through the list of Toolbars and then select the "my command bar" item.

  3. Click Delete and then click OK.

  4. Click Close.

REFERENCES

For more information about CommandBars, click the Office Assistant, type "commandbar", click Search, and then click to view "CommandBar Object".


Additional query words: 97 XL97
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: March 13, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.