I have a code (which I copied and edited it slightly from How do you extract email addresses from the 'To' field in outlook? and another source which disappeared) that is currently functioning to extract the dates, and names & email addresses (Sender, Recipients, CC) of every email in a specific mail folder. However, after cross-referencing with the manually exported email list from Outlook (via the Import & Export Wizard), I realized that there were about 30 emails which had an error in the VBA exported table. For each of these rows which contained information from their respective email, the names in the "Sender", "To", and "CC" were blank, while the corresponding email columns were just a copy of the previous email which was successfully extracted. So far, from what I observed, the similarity between these emails is that they are for Microsoft Team meetings (e.g., invitation to attend a teams meeting). The image below depicts the error:
Furthermore, one individual had two email addresses (each with different domains). However, only one of the email address was successfully exported out for its corresponding email; the emails which contained the other email address failed to export that particular email address, even though their name was still there. Similarly, the error was like this:
Kindly help me to identify and if possible, rectify the above problems as I have little to no experience in coding. Thanks in advance. The code is as follows -
Option Explicit
Sub GetEmail()
Dim appOutlook As Object
Dim olNs As Object
Dim olFolder As Object
Dim olItem As Object
Dim iRow As Integer
' Get/create Outlook Application
On Error Resume Next
Set appOutlook = GetObject(, "Outlook.Application")
If appOutlook Is Nothing Then
Set appOutlook = CreateObject("Outlook.Application")
End If
On Error GoTo 0
Set olNs = appOutlook.GetNamespace("MAPI")
'Set olFolder = olNs.GetDefaultFolder(6) ' 6 == Inbox for some reason
Set olFolder = olNs.Session.PickFolder
' Clear
ThisWorkbook.ActiveSheet.Cells.Delete
' Build headings:
Range("A1:G1") = Array("From:", "To:", "CC:", "SenderEmailAddress", "RecipientEmailAddress", "CCEmailAddress", "Date")
For iRow = 1 To olFolder.Items.Count
Cells(iRow + 1, 1) = olFolder.Items.Item(iRow).Sender
On Error Resume Next
Cells(iRow + 1, 2) = olFolder.Items.Item(iRow).To
Cells(iRow + 1, 3) = olFolder.Items.Item(iRow).CC
Dim Arr As Variant: Arr = EmailAddressInfo(olFolder.Items(iRow))
Cells(iRow + 1, 4) = Arr(olOriginator)
Cells(iRow + 1, 5) = Arr(olTo)
Cells(iRow + 1, 6) = Arr(olCC)
Cells(iRow + 1, 7) = olFolder.Items.Item(iRow).ReceivedTime
Next iRow
End Sub
Private Function EmailAddressInfo(olItem As MailItem) As Variant
If olItem.Class <> olMail Then Exit Function
On Error GoTo ExitFunction
Dim olRecipient As Outlook.Recipient
Dim olEU As Outlook.ExchangeUser
Dim olEDL As Outlook.ExchangeDistributionList
Dim ToAddress, CCAddress, Originator, email As String
With olItem
Select Case UCase(.SenderEmailType)
Case "SMTP": Originator = .SenderEmailAddress
Case Else
Set olEU = .Sender.GetExchangeUser
If Not olEU Is Nothing Then Originator = olEU.PrimarySmtpAddress
End Select
End With
For Each olRecipient In olItem.Recipients
With olRecipient
Select Case .AddressEntry.AddressEntryUserType
Case olSmtpAddressEntry 'OlAddressEntryUserType.
email = .Address
Case olExchangeDistributionListAddressEntry, olOutlookDistributionListAddressEntry
Set olEDL = .AddressEntry.GetExchangeDistributionList
email = IIf(Not olEDL Is Nothing, olEDL.PrimarySmtpAddress, "")
Case Else
Set olEU = .AddressEntry.GetExchangeUser
email = IIf(Not olEU Is Nothing, olEU.PrimarySmtpAddress, "")
End Select
If email <> "" Then
Select Case .Type
Case olTo: ToAddress = ToAddress & email & ";"
Case olCC: CCAddress = CCAddress & email & ";"
End Select
End If
End With
Next
EmailAddressInfo = Array(Originator, ToAddress, CCAddress)
ExitFunction:
End Function