XL2000: How to Place a Check Mark Next to a Custom Menu Item (213735)



The information in this article applies to:

  • Microsoft Excel 2000

This article was previously published under Q213735

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.

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: 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.

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.

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbdtacode kbhowto kbinfo kbProgramming KB213735