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 Place a Check Mark Next to a Custom Menu Item


View products that this article applies to.

This article was previously published under Q213735

↑ Back to the top


Summary

When you create custom menus, you may want to add a check mark next to a particular menu item to show that the item is selected. For example, when you point to Filter on the Data menu and then click AutoFilter, a check mark is placed next to AutoFilter to show that the AutoFilter is currently turned on.

This article shows you how to programmatically add a check mark next to a custom menu item, using the new CommandBars object model.

NOTE: You cannot programmatically add a check mark next to a built-in menu item; you can add check marks only to custom items.

↑ 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 The msoButtonUp and msoButtonDown states of a menu item can be used to show or hide a check mark. To add a new custom menu to a worksheet and enable it to display a check mark when selected, follow these steps:
  1. Start Microsoft Excel and open a new workbook.
  2. On the Tools menu, point to Macro, and then click Visual Basic Editor.
  3. On the Insert menu, click Module.
  4. Type or paste the following code:
    Sub checked_menuitem()    'create a new menu item
    
        '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()    'show or hide check mark and perform other actions
    
        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 macro creates a docked CommandBar with a single menu named "my menu."
  6. Return to Excel through the taskbar or by pressing ALT+F11.
  7. Click the my menu drop-down list, and then click my menu item.

    Notice the message box stating that the menu is now selected, as well as the check mark next to my menu item.
  8. Repeat step 7 and notice the appearance of the message box and menu item.

Removing the Custom Command Bar

  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.

↑ Back to the top


References

For more information about Command Bars, click Microsoft Excel Help on the Help menu, type commandbar property in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

↑ Back to the top


Keywords: KB213735, kbprogramming, kbinfo, kbhowto, kbdtacode

↑ Back to the top

Article Info
Article ID : 213735
Revision : 8
Created on : 11/23/2006
Published on : 11/23/2006
Exists online : False
Views : 318