Microsoft KB Archive/199563

From BetaArchive Wiki

Article ID: 199563

Article Last Modified on 9/12/2006



APPLIES TO

  • Microsoft Excel 98 for Macintosh



This article was previously published under Q199563


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 programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements.

  1. Save and close any open workbooks and then open a new workbook.
  2. Start the Visual Basic Editor (press OPTION+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".
  6. Activate Microsoft Excel (press OPTION+F11).
  7. 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.
  8. 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, from the Visual Basic Editor, click the Office Assistant, type commandbar object, click Search, and then click to view "CommandBar Object."

For more information about getting help with Visual Basic for Applications, click the article number below to view the article in the Microsoft Knowledge Base:

163435 VBA: Programming Resources for Visual Basic for Applications


For more information about running sample code, click the article number below to view the article in the Microsoft Knowledge Base:

181058 OFF98: How to Run Sample Macro Code from Knowledge Base Articles



Additional query words: 97 XL97 OFF98 XL98 vba

Keywords: kbhowto kbinfo kbualink97 kbcode KB199563