How to: Share Contact Information Including the Business Card
|Outlook Developer Reference|
You can use the ForwardAsVcard and ForwardAsBusinessCard method of the ContactItem object to create a new MailItem object that contains the contact information from the specified ContactItem attached as a vCard (.vcf) file, or you can use the AddBusinessCard method of the MailItem object to attach the contact information for a specified ContactItem as a vCard file. If you use the ForwardAsBusinessCard or AddBusinessCard methods, an image of the business card is also appended to the body of the mail item if the BodyFormat property of the MailItem object is set to olFormatHTML.
This sample provides the following two routines to customize the selection context menu, allowing the user to forward the contact information for a selected Outlook contact item as both an included business card image and an attached vCard file:
- 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 retrieves the selected contact item and calls the ForwardAsBusinessCard method when the menu item is selected.
The event handler routine performs the following actions:
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 contact item is selected in the active explorer.
It then configures the CommandBarButton:
- The BeginGroup property is set so that the menu item is displayed in the selection context menu in a separate group.
- The Caption property is set so that the title of the menu item reads "Forward."
- The Tag property is set to "ForwardContactItem", 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 contact item, which is retrieved from the Selection parameter of the ItemContextMenuDisplay event.
- The OnAction property is set to "Project1.ThisOutlookSession.ForwardContactItem", 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 Forward menu item appended to the end of the selection 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:
- 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.
- 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.
- If a value was retrieved, an object reference representing the selected item is retrieved by using the GetItemFromID method of the NameSpace object.
- If a valid Outlook item was retrieved, the sample then calls the ForwardAsBusinessCard method of the ContactItem object to create a new MailItem object containing the contact information.
- Finally, the sample calls the Display method of the MailItem object to display the mail item.