MailItem.Sender property (Outlook)
expression A variable that represents a MailItem object.
In a session where multiple accounts are defined in the profile, you can set this property to specify the account from which to send a mail item. Set this property to the AddressEntry object of the user that is represented by the CurrentUser property of a specific account.
If you set the Sender property to an AddressEntry that does not have permissions to send messages on that account, Microsoft Outlook will raise an error.
Michael Bauer provided the following code example. 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 following VBA code example shows how to display the details of the sender of an email. 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, the user should 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
Support and feedback
Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.