This Outlook VBA code searches all emails in my Outlook subfolder, and then pulls the "Subject", "Date", "Creation Time", and "Body" of the email into an Excel file.
How can I implement some code that will look at emails after a certain date (e.g. 10/1/2022)?
My current code:
Sub List_Email_Info()
'Create excel object variables
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim i As Long 'Row Tracker
Dim arrHeader As Variant
'Create outlook object variables
Dim olNS As NameSpace
Dim olInboxFolder As MAPIFolder
Dim olItems As Items
Dim olMailItem As MailItem
'store header names
arrHeader = Array("Date Created", "Subject", "Sender's Name", "Body")
'Create excel object's isntance
Set xlApp = CreateObject("excel.Application")
xlApp.Visible = True
Set xlWB = xlApp.Workbooks.Add
'Set outlook variables
Set olNS = GetNamespace("MAPI")
Set olInboxFolder = olNS.GetDefaultFolder(olFolderInbox).Folders("Law360 Alerts")
Set olItems = olInboxFolder.Items
'Assign role value to i variable
i = 1
On Error Resume Next
xlWB.Worksheets(1).Range("A1").Resize(1, UBound(arrHeader) + 1).Value = arrHeader
'iteriate each item from the olItems object
For Each olMailItem In olItems
xlWB.Worksheets(1).Cells(i + 1, "A").Value = olItems(i).CreationTime
xlWB.Worksheets(1).Cells(i + 1, "B").Value = olItems(i).Subject
xlWB.Worksheets(1).Cells(i + 1, "C").Value = olItems(i).SenderName
xlWB.Worksheets(1).Cells(i + 1, "D").Value = olItems(i).Body
i = i + 1
Next olMailItem
'Autofit columns
xlWB.Worksheets(1).Cells.EntireColumn.AutoFit
'Display a messagebox when complete
MsgBox "Export Complete.", vbInformation
'Empty out the objects
Set xlWB = Nothing
Set xlApp = Nothing
Set olItems = Nothing
Set olInboxFolder = Nothing
Set olNS = Nothing
End Sub