Microsoft KB Archive/213735

= XL2000: How to Place a Check Mark Next to a Custom Menu Item =

Article ID: 213735

Article Last Modified on 11/23/2006

-

APPLIES TO


 * Microsoft Excel 2000 Standard Edition

-



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:

https://partner.microsoft.com/global/30000104

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:  Start Microsoft Excel and open a new workbook. On the Tools menu, point to Macro, and then click Visual Basic Editor. On the Insert menu, click Module.  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  Run the checked_menuitem macro.

This macro creates a docked CommandBar with a single menu named "my menu." Return to Excel through the taskbar or by pressing ALT+F11.</li> 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.</li> Repeat step 7 and notice the appearance of the message box and menu item.</li></ol>

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.

<div class="references_section">