Partilhar via


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:

  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 contact item is selected in the active explorer.

  2. 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:

  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 then calls the ForwardAsBusinessCard method of the ContactItem object to create a new MailItem object containing the contact information.
  5. Finally, the sample calls the Display method of the MailItem object to display the mail item.
  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
        If Selection.Item(1).Class = olContact Then
            ' Add a new button to the bottom of the CommandBar
            ' (which represents the item context menu.)
            Set objButton = CommandBar.Controls.Add( _
                msoControlButton)
            
            ' Configure the button to call the
            ' ForwardContactItem 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
                .BeginGroup = True
                .Caption = "Forward"
                .Tag = "ForwardContactItem"
                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 = _
                    "Project1.ThisOutlookSession.ForwardContactItem"
            End With
        End If
    End If
    
EndRoutine:
    Exit Sub
    
ErrRoutine:
    MsgBox Err.Number & " - " & Err.Description, _
        vbOKOnly Or vbCritical, _
        "Application_ItemContextMenuDisplay"
    GoTo EndRoutine
End Sub

Private Sub ForwardContactItem()

Dim objNamespace As NameSpace
Dim objContactItem As ContactItem
Dim objMailItem As MailItem
Dim strEntryID As String

On Error GoTo ErrRoutine

' Retrieve the value of the Parameter property from the
' control that called this routine.
strEntryID = _
    Application.ActiveExplorer.CommandBars.ActionControl.Parameter

' 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."
Else
    ' Fetch an item reference using the specified entry ID.
    Set objNamespace = Application.GetNamespace("MAPI")
    Set objContactItem = objNamespace.GetItemFromID(strEntryID)
    
    If objContactItem Is Nothing Then
        MsgBox "A reference for the Outlook item " & _
            "could not be retrieved."
    Else
        ' Forward the contact item, including a business card
        ' image, and display the new MailItem object.
        Set objMailItem = objContactItem.ForwardAsBusinessCard
        objMailItem.Display
    End If
End If

EndRoutine: Set objItem = Nothing Set objNamespace = Nothing Exit Sub

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