Export (0) Print
Expand All

How to: Hide or Disable a Menu Item on a Context Menu

Office 2007

The following context menu events provide a reference to the CommandBar object that represents the context menu to be displayed:

You can set the Visible and Enabled properties of command bar controls, such as the CommandBarButton object, to respectively hide or disable menu items on a context menu. If you want to prevent the display of menu items provided by Outlook, you should hide, rather than remove, such menu items by setting the Visible property for menu items to be hidden to False.

The sample temporarily modifies the context menu for a store, such as an Exchange mailbox or an Outlook data (.pst) file, so that it cannot be moved to the Favorites navigation group within Outlook. The sample does this by alternatively hiding or disabling the Add to Favorite Folders menu item within the store context menu. The sample performs the following actions:

  1. The sample first iterates through each control in the CommandBar object provided by the CommandBar parameter of the StoreContextMenuDisplay event.
  2. If the Add to Favorite Folders menu item, a CommandBarButton with an Id property value of 7888, is present in the context menu, the sample then sets the Visible property of that menu item to the value of the variable, blnHiddenOrDisabled.
  3. It then sets the Enabled property of that menu item to the opposite value of the variable, blnHiddenOrDisabled. The sample sets the Visible and Enabled properties in this way so that the menu item is either not visible, or visible but disabled, alternating between the two states each time the event occurs in Outlook.
  4. After completing the iteration, the sample finally sets the blnHiddenOrDisabled Boolean variable to its opposite value, so that the sample can alternate between the two states on each subsequent StoreContextMenuDisplay event.
Private Sub Application_StoreContextMenuDisplay( _
    ByVal CommandBar As Office.CommandBar, _
    ByVal Store As Store)
    
    On Error GoTo ErrRoutine
    
    Static blnHiddenOrDisabled As Boolean
    Dim objObject As Object
    
    For Each objObject In CommandBar.Controls
        ' If the Add to Favorite Folders button
        ' is present, remove it from the context menu.
        If objObject.ID = 7888 Then
            objObject.Visible = blnHiddenOrDisabled
            objObject.Enabled = Not blnHiddenOrDisabled
        End If
        
        blnHiddenOrDisabled = Not blnHiddenOrDisabled
    Next
    
EndRoutine:
    On Error GoTo 0
    Exit Sub

ErrRoutine:
    MsgBox Err.Number & " - " & Err.Description, _
    vbOKOnly Or vbCritical, _
    "Application_StoreContextMenuDisplay"
    
    GoTo EndRoutine

End Sub


Community Additions

ADD
Show:
© 2014 Microsoft