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. 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 -
In Visual Studio 2010, create an add-in for Outlook 2010. Name the new project ExtendCC. -
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). -
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 -
First, change the existing xmlns value in the XML file to the following value.
http://schemas.microsoft.com/office/2009/07/customui
-
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>
-
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 -
Modify the GetCustomUI method to be more specific about requests for the context menu XML.
Select Case ribbonID
Case "Microsoft.Mso.IMLayerUI"
Return GetResourceText("ExtendCC.ContactCardContextMenu.xml")
Case Else
Return String.Empty
End Select
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 -
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
-
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
-
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
-
Press F5 to start your add-in in debug mode. -
Open an e-mail item in your Inbox. -
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. | Watch the video
Length: 00:5:37 Grab the Code |