I have this code below which loops through the inbox, searching for a specific e-mail address entered on the worksheet's column E. it will return the last e-mail sent date to column b.
Sub ()
Dim olApp As Outlook.Application
Dim olNs As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder
Dim olMail As Outlook.MailItem
Dim eFolder As Outlook.Folder
Dim i As Long
Dim wb As Workbook
Dim ws As Worksheet
Dim icounter As Long
Dim lrow As Long
Set wb = ActiveWorkbook
Set ws = wb.Worksheets("-")
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
lastrow = ThisWorkbook.Worksheets("vlookup").Cells(Rows.Count, "E").End(xlUp).Row
Set olFolder = olNs.GetDefaultFolder(olFolderInbox)
For i = olFolder.Items.Count To 1 Step -1
If TypeOf olFolder.Items(i) Is MailItem Then
Set olMail = olFolder.Items(i)
For icounter = 2 To lastrow
If InStr(olMail.SenderEmailAddress, ws.Cells(icounter, 5).Value) > 0 Then 'qualify the cell
With ws
lrow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("B" & lrow + 1).Value = olMail.ReceivedTime
.Range("A" & lrow + 1).Value = olMail.SenderEmailAddress
End With
End If
Next icounter
End If
Next i
Set olFolder = Nothing
End Sub
I'm not sure how to loop through the subfolders. I've checked SO and found this code below from Can I iterate through all Outlook emails in a folder including sub-folders?
Private Sub processFolder(ByVal oParent As Outlook.MAPIFolder)
Dim oFolder As Outlook.MAPIFolder
Dim oMail As Outlook.MailItem
For Each oMail In oParent.Items
'Get your data here ...
Next
If (oParent.Folders.Count > 0) Then
For Each oFolder In oParent.Folders
processFolder oFolder
Next
End If
End Sub
But i've never used private subs so I don't know how to combine them..
Also found this which is the combined version using the private sub i found above, but I had no luck in translating it to my code. Outlook VBA Importing Emails from Subfolders into Excel