How to get the sender’s email address from one or more emails in Outlook?
Have you ever tried to extract the email address from the “From” field of one or more received emails in Outlook? This article provides a VBA code to help you handle this task.
Please run the following VBA code to extract the email address from the “From” field of one or more received emails in Outlook.
1. Open an email folder, select an email message from which you want to get the sender’s email address. Press the Alt + F11 keys to open the Microsoft Visual Basic for Applications window.
Tips: To select multiple emails, please hold down the Ctrl key and then select the emails one by one.
2. In the Microsoft Visual Basic for Applications window, click Insert > Module, then copy the following VBA code to the Module (code) window.
VBA code: extract the sender’s email address from one or more emails in Outlook
Sub GetSmtpAddressOfSelectionEmail() Dim xExplorer As Explorer Dim xSelection As Selection Dim xItem As Object Dim xMail As MailItem Dim xAddress As String Dim xFldObj As Object Dim FilePath As String Dim xFSO As Scripting.FileSystemObject On Error Resume Next Set xExplorer = Application.ActiveExplorer Set xSelection = xExplorer.Selection For Each xItem In xSelection If xItem.Class = olMail Then Set xMail = xItem xAddress = xAddress & VBA.vbCrLf & " " & GetSmtpAddress(xMail) End If Next If MsgBox("Sender SMTP Address is: " & xAddress & vbCrLf & vbCrLf & "Do you want to export the address list to a txt file? ", vbYesNo, "Kutools for Outlook") = vbYes Then Set xFldObj = CreateObject("Shell.Application").BrowseforFolder(0, "Select a Folder", 0, 16) Set xFSO = New Scripting.FileSystemObject If xFldObj Is Nothing Then Exit Sub FilePath = xFldObj.Items.Item.Path & "\Address.txt" Close #1 Open FilePath For Output As #1 Print #1, "Sender SMTP Address is: " & xAddress Close #1 Set xFSO = Nothing Set xFldObj = Nothing MsgBox "Address list has been exported to：" & FilePath, vbOKOnly + vbInformation, "Kutools for Outlook" End If End Sub Function GetSmtpAddress(Mail As MailItem) Dim xNameSpace As Outlook.NameSpace Dim xEntryID As String Dim xAddressEntry As AddressEntry Dim PR_SENT_REPRESENTING_ENTRYID As String Dim PR_SMTP_ADDRESS As String Dim xExchangeUser As exchangeUser On Error Resume Next GetSmtpAddress = "" Set xNameSpace = Application.Session If Mail.sender.Type <> "EX" Then GetSmtpAddress = Mail.sender.Address Else PR_SENT_REPRESENTING_ENTRYID = "http://schemas.microsoft.com/mapi/proptag/0x00410102" xEntryID = Mail.PropertyAccessor.BinaryToString(Mail.PropertyAccessor.GetProperty(PR_SENT_REPRESENTING_ENTRYID)) Set xAddressEntry = xNameSpace.GetAddressEntryFromID(xEntryID) If xAddressEntry Is Nothing Then Exit Function If xAddressEntry.AddressEntryUserType = olExchangeUserAddressEntry Or xAddressEntry.AddressEntryUserType = olExchangeRemoteUserAddressEntry Then Set xExchangeUser = xAddressEntry.GetExchangeUser() If xExchangeUser Is Nothing Then Exit Function GetSmtpAddress = xExchangeUser.PrimarySmtpAddress Else PR_SMTP_ADDRESS = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E" GetSmtpAddress = xAddressEntry.PropertyAccessor.GetProperty(PR_SMTP_ADDRESS) End If End If End Function
3. Click Tools > References, then check the Microsoft Scripting Runtime box in the References – Project1 dialog box.
4. Press the F5 key to run the code. Then a Kutools for Outlook dialog box pops up, listing all the sender’s email addresses of the selected emails.
5. After clicking the Yes button, a Browse For Folder dialog box pops up. Please choose a folder to save the file and click the OK button.
6. Finally, a Kutools for Outlook dialog box will pop up, telling you the path of the exported file. Click OK to close it.
7. Go to the folder where the exported file is saved and open the .txt file named Address to see the sender’s email addresses of the selected emails.
Best Office Productivity Tools
Kutools for Outlook - Over 100 Powerful Features to Supercharge Your Outlook
Over 100 Features Await Your Exploration! Click Here to Discover More.