Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

How to: Customize an Item Context Menu

Office 2007

You can use the ItemContextMenuDisplay event of the Application object to customize an item context menu or selection context menu before the menu is displayed to the user.

This sample provides the following two routines to customize the selection context menu, allowing the user to display the values of certain properties for a selected Outlook item:

  • An event handler routine that, when the ItemContextMenuDisplay event occurs, adds a new menu item to the bottom of the selection context menu.
  • An action routine, associated with the menu item, that displays some of the properites associated with the selected Outlook item when the menu item is selected.

The event handler routine performs the following actions:

  1. The sample first creates a CommandBarButton object in the command bar returned by the CommandBar parameter of the ItemContextMenuDisplay event, but only if one and only one Outlook item is selected in the active explorer.
  2. It then configures the CommandBarButton:
    • The Caption property is set so that the title of the menu item reads "Display metadata."
    • The Tag property is set to "DisplayItemMetadata", the name of the action that the menu item will take when selected.
    • The Parameter property is set to the value of the EntryID for the selected Outlook item, which is retrieved from the Selection parameter of the ItemContextMenuDisplay event.
    • The OnAction property is set to "Project1.ThisOutlookSession.DisplayItemMetadata", the full name of the action that the menu item will take when selected. The value of this property corresponds to the full name of the action routine in this sample.

After the event handler routine finishes execution, the item context menu is displayed. If the Display metadata menu item appended to the end of the item context menu is selected, the action routine that was specified in the OnAction property of the CommandBarButton representing that menu item is called. The action routine then performs the following actions:

  1. The sample first retrieves the entry ID of the selected item from the Parameter property of the object returned by the Application.ActiveExplorer.CommandBars.ActionControl property.
  2. It then checks if a value was retrieved from the Parameter property. If no value was retrieved, a message is displayed indicating that the selected Outlook item could not be retrieved.
  3. If a value was retrieved, an object reference representing the selected item is retrieved by using the GetItemFromID method of the NameSpace object.
  4. If a valid Outlook item was retrieved, the sample finally displays the values of the MessageClass and Size properties.
Sub Application_ItemContextMenuDisplay( _
    ByVal CommandBar As Office.CommandBar, _
    ByVal Selection As Selection)
    Dim objButton As Office.CommandBarButton
    On Error GoTo ErrRoutine
    If Selection.Count = 1 Then
        ' Add a new button to the bottom of the CommandBar
        ' (which represents the item context menu.)
        Set objButton = CommandBar.Controls.Add( _
        ' Configure the button to call the
        ' DisplayItemMetadata routine when
        ' clicked. The Parameter property of the
        ' button is set to the value of the
        ' EntryID property for the selected
        ' item, if possible.
        With objButton
            .Caption = "&Display metadata"
            .FaceId = 1000
            .Tag = "DisplayItemMetadata"
            If Not IsNull(Selection.Item(1)) Then
                On Error GoTo 0
                ' Just in case the item selected
                ' doesn't have a valid EntryID.
                .Parameter = Selection.Item(1).EntryID
                On Error GoTo ErrRoutine
            End If
            .OnAction = _
        End With
    End If
    Exit Sub
    MsgBox Err.Number & " - " & Err.Description, _
        vbOKOnly Or vbCritical, _
    GoTo EndRoutine
End Sub

Private Sub DisplayItemMetadata()

    Dim objNamespace As NameSpace
    Dim objItem As Object
    Dim strEntryID As String
    On Error GoTo ErrRoutine
    ' Retrieve the value of the Parameter property from the
    ' control that called this routine.
    strEntryID = _
    ' If there's no entry ID, we can't easily retrieve the item.
    If strEntryID = "" Then
        MsgBox "An entry ID could not be retrieved from " & _
            "the selected menu item."
        ' Fetch an item reference using the specified entry ID.
        Set objNamespace = Application.GetNamespace("MAPI")
        Set objItem = objNamespace.GetItemFromID(strEntryID)
        If objItem Is Nothing Then
            MsgBox "A reference for the Outlook item " & _
                "could not be retrieved."
            ' Display information about the item.
            MsgBox "Message Class: " & objItem.MessageClass & vbCrLf & _
                "Size:          " & objItem.Size
        End If
    End If

    Set objItem = Nothing
    Set objNamespace = Nothing
    Exit Sub

    MsgBox Err.Number & " - " & Err.Description, _
        vbOKOnly Or vbCritical, _
    GoTo EndRoutine
End Sub

Community Additions

© 2015 Microsoft