Customizing the Context Menu of a Contact Card in Outlook 2010

Office Visual How To

Summary:  Create an add-in to add a custom item to the context menu of the new Contact Card feature in Microsoft Outlook 2010.

Applies to: Office 2010 | Outlook 2010 | Visual Studio

Published:   December 2009

Provided by:   Brian A. Randell, MCW Technologies, LLC

Overview

Microsoft Outlook 2010 introduces a new user interface element called the Contact Card. The Contact Card exposes a context menu that you can use to add your own menu items. This Visual How To explains how to create an add-in by using Visual Studio Tools for Office 2010 to customize the Contact Card context menu.

Code It

This Visual How To shows how to add a menu item to the Contact Card context menu. Choosing the menu item displays the SMTP address of the selected contact.

You can use a Visual Studio Tools for Office add-in to run your compiled code inside Outlook and to interact with the Outlook object model.

To create the add-in

  1. In Visual Studio 2010, create an add-in for Outlook 2010. Name the new project ExtendCC.

  2. In the Solution Explorer window, right-click the project, select Add from the context menu, and then click New Item. In the Add New Item dialog box, Common Items, select Office, and then select Ribbon (XML).

  3. Change the Name to ContactCardContextMenu and then click Add. Visual Studio adds two files; a source file and an XML file.

To customize the Microsoft Office Fluent UI context menu, you must use a Ribbon (XML) type, which requires an XML file that describes the user interface elements and a source file that contains your code.

To customize the context menu item

  1. First, change the existing xmlns value in the XML file to the following value.

    https://schemas.microsoft.com/office/2009/07/customui
  2. Next, replace all the child XML inside the customUI element by using the following code.

    <contextMenus>
      <contextMenu idMso="ContextMenuContactCardRecipient">
        <button id="MyContextMenuContactCardRecipient"
            label="Get Contact SMTP Address"
            onAction="OnGetContactSmtpAddrClick"/>
      </contextMenu>
    </contextMenus>
  3. Save your work.

You must modify the existing GetCustomUI method so that it passes your XML to Outlook 2010 when Outlook 2010 asks for customizations to the Contact Card context menu.

To display your customization in Outlook

At this point, you can add your custom method. In this example, you access the current contact and try to determine the contact's SMTP address.

To call your custom method

  1. Next, add a method to try to find the contact's SMTP address by using the IMsoContactCard object.

    Note

    The IMsoContactCard object is defined in the primary interop assembly, Office.dll, of the Microsoft Office object library, not in Outlook 2010. Visual Studio Tools for Office automatically references the assembly for you.

    Private Function GetSmtpAddress(ByVal card As Office.IMsoContactCard) As String
      If card.AddressType =
          Office.MsoContactCardAddressType.msoContactCardAddressTypeOutlook Then
        Dim host As Outlook.Application = Globals.ThisAddIn.Application
        Dim ae As Outlook.AddressEntry =
            host.Session.GetAddressEntryFromID(card.Address)
    
    If (ae.AddressEntryUserType =
      Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry _
      OrElse ae.AddressEntryUserType = _
      Outlook.OlAddressEntryUserType.olExchangeRemoteUserAddressEntry) Then
          Dim ex As Outlook.ExchangeUser =
            ae.GetExchangeUser()
          Return ex.PrimarySmtpAddress
        Else
          Throw New Exception("Valid address entry not found.")
        End If
      Else
        Return card.Address
      End If
    End Function
  2. Add the code to call when users select the context menu item.

    Try
      Dim card As Office.IMsoContactCard =
          TryCast(control.Context, Office.IMsoContactCard)
    
       If card IsNot Nothing Then
        MsgBox(GetSmtpAddress(card))
       Else
         MsgBox("Unable to access contact card")
       End If
      Catch ex As Exception
        MsgBox(ex.Message)
    End Try
  3. Finally, add code in the ThisAddIn class to enable Outlook to load your code.

    Protected Overrides Function CreateRibbonExtensibilityObject() As  _
      Microsoft.Office.Core.IRibbonExtensibility
      Return New ContactCardContextMenu()
    End Function
  4. Press F5 to start your add-in in debug mode.

  5. Open an e-mail item in your Inbox.

  6. Right-click on a sender or a recipient, and then select the Get Contact SMTP Address menu item to try it out.

Although this is a simple example, it shows that after you know how to customize the context menu, you have access to a rich set of solutions.

See It

Watch the video

> [!VIDEO https://www.microsoft.com/en-us/videoplayer/embed/0a915db6-71cc-4dfc-88ee-47e19856ba19]

Length: 00:5:37

Click to grab code

Grab the Code

Explore It