Share via


MVP Michael Bauer Contributed a Code Example for a New Outlook 2010 Property

Michael Bauer* recently contributed a code example that shows a simple but common way to use the new MailItem proeprty, Sender, in Microsoft Outlook 2010.

*Michael is a Microsoft Most Valuable Professional with expertise in developing Outlook solutions in Visual Basic and Visual Basic for Applications (VBA). Michael maintains a professional site at VBOffice.net.

The new MailItem property, Sender, allows you to get or set an AddressEntry object that corresponds to the user of the account from which the mail item is sent. Using this AddressEntry object, you can use the Details property, or the GetContact method if the sender is saved as a contact, to obtain information of the sender.

The following VBA code example shows how to display the details of the sender of an e-mail. If the sender corresponds to a contact in the user's Outlook Contacts Address Book (CAB), the code example displays information about that contact in an inspector. If the sender is not a contact in the user's CAB, the code example displays details from  the user's address entry (taken from the transport provider's address book container) in a dialog box.

To display information about a sender, this code example requires the user to have selected a MailItem in the explorer. The code example also checks whether the selected MailItem has been sent, because the Sender property is defined only if the Mailtem has been sent. The example then  accesses the Sender property to obtain the AddressEntry object that corresponds to the sender of that mail item, and displays the contact information, if it exists; otherwise, the example displays the address entry details.

Public Sub DisplaySenderDetails()
    Dim Explorer As Outlook.Explorer
    Dim CurrentItem As Object
    Dim Sender As Outlook.AddressEntry
    Dim Contact As Outlook.ContactItem
 
    Set Explorer = Application.ActiveExplorer
 
    ' Check whether any item is selected in the current folder.
    If Explorer.Selection.Count Then
   
        ' Get the first selected item.
        Set CurrentItem = Explorer.Selection(1)
   
        ' Check for the type of the selected item as only the
        ' MailItem object has the Sender property.
        If CurrentItem.Class = olMail Then
            Set Sender = CurrentItem.Sender
     
            ' There is no sender if the item has not been sent yet.
            If Sender Is Nothing Then
                MsgBox "There's no sender for the current email", vbInformation
                Exit Sub
            End If
     
            Set Contact = Sender.GetContact
     
            If Not Contact Is Nothing Then
                ' The sender is stored in the contacts folder,
                ' so the contact item can be displayed.
               Contact.Display
       
            Else
                ' If the contact cannot be found, display the
                ' address entry in the properties dialog box.
                Sender.Details 0
            End If
        End If
    End If
End Sub

*Michael is a Microsoft Most Valuable Professional with expertise in developing Outlook solutions in Visual Basic and Visual Basic for Applications (VBA). Michael maintains a professional site at VBOffice.net.