TheBitDoctor
Programmer
I've diverted a set of messages into a folder based on the subject line. Now I want to search a database using the senders email address but I can't get to the information I need using Outlook VBA. I'm using this code:
Dim ol As New Outlook.Application
Dim ns As Outlook.NameSpace
Dim InboxFolder As Outlook.MAPIFolder
Dim SubFolder As Outlook.MAPIFolder
Dim MsgItem As Outlook.MailItem
Dim TheBody As String
Dim SenderEmail As String
Set ns = ol.GetNamespace("MAPI")
Set InboxFolder = ns.GetDefaultFolder(olFolderInbox)
Set SubFolder = InboxFolder.Folders("Diverted Msgs")
For Each MsgItem In SubFolder.Items
SenderEmail = MsgItem.SenderName
TheBody = MsgItem.Body
... look up senders email address in database
Next MsgItem
The problem I'm having is with the SenderName or SentOnBehalfOfName properties. On those messages with a text prefix ie "FirstName Lastname" <First.Last@somewhere.com> all these fields contain is the quoted portion and I need the actual email address inside the angle brackets. The information I need is in the "Return-Path" field of the email message header but Outlook VBA doesn't seem to provide access to either the message header or the <...> portion of the from field.
Does anyone know a consistent method for finding the email address that a message was sent from using Outlook VBA?
Dim ol As New Outlook.Application
Dim ns As Outlook.NameSpace
Dim InboxFolder As Outlook.MAPIFolder
Dim SubFolder As Outlook.MAPIFolder
Dim MsgItem As Outlook.MailItem
Dim TheBody As String
Dim SenderEmail As String
Set ns = ol.GetNamespace("MAPI")
Set InboxFolder = ns.GetDefaultFolder(olFolderInbox)
Set SubFolder = InboxFolder.Folders("Diverted Msgs")
For Each MsgItem In SubFolder.Items
SenderEmail = MsgItem.SenderName
TheBody = MsgItem.Body
... look up senders email address in database
Next MsgItem
The problem I'm having is with the SenderName or SentOnBehalfOfName properties. On those messages with a text prefix ie "FirstName Lastname" <First.Last@somewhere.com> all these fields contain is the quoted portion and I need the actual email address inside the angle brackets. The information I need is in the "Return-Path" field of the email message header but Outlook VBA doesn't seem to provide access to either the message header or the <...> portion of the from field.
Does anyone know a consistent method for finding the email address that a message was sent from using Outlook VBA?