The following functions should be adaptable for what you require. A listener for new e-mail will be needed (which I assume you have), but you can then pass the MailItem
into the EmailAddressInfo
function which returns a 3 dimension array for the sender, To and CC addresses. I've included a test function which can be used to check this
The code should be expandable in the case of unknown e-mail types through the use of the CASE
declarations
Private Const olOriginator As Long = 0, olTo As Long = 1, olCC As Long = 2, olBCC As Long = 3
Function PrintEmailAddresses(olItem As MailItem)
If olItem.Class <> olMail Then Exit Function
Dim Arr As Variant: Arr = EmailAddressInfo(olItem)
Debug.Print "Sender: " & Arr(0)
Debug.Print "To Address: " & Arr(1)
Debug.Print "CC Address: " & Arr(2)
End Function
Private Function EmailAddressInfo(olItem As MailItem) As Variant
If olItem.Class <> olMail Then Exit Function
Dim olRecipient As Outlook.Recipient
Dim ToAddress, CCAddress, Originator, email As String
With olItem
Select Case UCase(.SenderEmailType)
Case "SMTP": Originator = .SenderEmailAddress
Case Else: Originator = .Sender.GetExchangeUser.PrimarySmtpAddress
End Select
End With
For Each olRecipient In olItem.Recipients
With olRecipient
Select Case UCase(.AddressEntry.Type)
Case "SMTP": email = .Address
Case Else: email = .AddressEntry.GetExchangeUser.PrimarySmtpAddress
End Select
Select Case .Type
Case olTo: ToAddress = ToAddress & email & ";"
Case olCC: CCAddress = CCAddress & email & ";"
End Select
End With
Next
EmailAddressInfo = Array(Originator, ToAddress, CCAddress)
End Function