How to get data from Outlook into Excel based on multiple criteria automatically with VBA. In the last video we learnt how to get data from Outlook into an Excel worksheet based on a specific date. Today we learn how to get data from Outlook into Excel based on a date range, date range and sender and date range and subject. Watch the video to learn how to use an IF function with multiple criteria:
Watch this video on YouTube.
Here’s the complete code:
Sub GetFromOutlook()
Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Namespace
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim i As Integer
Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace(“MAPI”)
Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox).Folders(“impMail”)
i = 1
For Each OutlookMail In Folder.Items
‘If OutlookMail.ReceivedTime >= Range(“email_Receipt_Date”).Value Then
‘If OutlookMail.ReceivedTime >= Range(“start_Date”).Value And OutlookMail.ReceivedTime <= Range(“end_Date”) And OutlookMail.Sender = Range(“Sender”) Then
If OutlookMail.ReceivedTime >= Range(“start_Date”).Value And OutlookMail.ReceivedTime <= Range(“end_Date”) And OutlookMail.Subject = Range(“Subject”) Then
‘If OutlookMail.ReceivedTime >= Range(“email_Receipt_Date”).Value And Date <= 28 – mar – 2018 Then
Range(“email_Subject”).Offset(i, 0).Value = OutlookMail.Subject
Range(“email_Subject”).Offset(i, 0).Columns.AutoFit
Range(“email_Subject”).Offset(i, 0).VerticalAlignment = xlTop
Range(“email_Date”).Offset(i, 0).Value = OutlookMail.ReceivedTime
Range(“email_Date”).Offset(i, 0).Columns.AutoFit
Range(“email_Date”).Offset(i, 0).VerticalAlignment = xlTop
Range(“email_Sender”).Offset(i, 0).Value = OutlookMail.SenderName
Range(“email_Sender”).Offset(i, 0).Columns.AutoFit
Range(“email_Sender”).Offset(i, 0).VerticalAlignment = xlTop
Range(“email_Body”).Offset(i, 0).Value = OutlookMail.Body
Range(“email_Body”).Offset(i, 0).Columns.AutoFit
Range(“email_Body”).Offset(i, 0).VerticalAlignment = xlTop
i = i + 1
End If
Next OutlookMail
Set Folder = Nothing
Set OutlookNamespace = Nothing
Set OutlookApp = Nothing
End Sub
how do I extract also the signature data from the email to excel sheet by each column for ex: phone, fax, name, email, company name, website etc?
>= Range is working fine however,
<= Range is not working for me. Code skips all below steps and loop without these (need help please):
Range(“email_Subject”).Offset(i, 0).Value = OutlookMail.Subject
Range(“email_Subject”).Offset(i, 0).Columns.AutoFit
Range(“email_Subject”).Offset(i, 0).VerticalAlignment = xlTop
Range(“email_Date”).Offset(i, 0).Value = OutlookMail.ReceivedTime
Range(“email_Date”).Offset(i, 0).Columns.AutoFit
Range(“email_Date”).Offset(i, 0).VerticalAlignment = xlTop
Range(“email_Sender”).Offset(i, 0).Value = OutlookMail.SenderName
Range(“email_Sender”).Offset(i, 0).Columns.AutoFit
Range(“email_Sender”).Offset(i, 0).VerticalAlignment = xlTop
Range(“email_Body”).Offset(i, 0).Value = OutlookMail.Body
Range(“email_Body”).Offset(i, 0).Columns.AutoFit
Range(“email_Body”).Offset(i, 0).VerticalAlignment = xlTop
I need a help in extracting emails from a particular email address when there are multiple email id’s in outlook with specific date range from inbox and sent items.
you can please contact me on [email protected]
Dear Sir, I wrote a code as below but it is posting me error runtime error 13 after getting some data from outlook to excel, Also it is not getting data of sepcific date mentioned in the code.
Sub checkresponsefromspecificsenderandsubjecttostopreminders()
Dim olml As Outlook.Application
Dim sndr As Outlook.Namespace
Dim fold As Outlook.Folder
‘Dim i As Object
Dim mi As Outlook.MailItem
Set olml = New Outlook.Application
Set sndr = olml.GetNamespace(“MAPI”)
Set fold = sndr.GetDefaultFolder(olFolderInbox)
Sheet2.Range(“A2”, Sheet2.Range(“A2”).End(xlDown).End(xlToRight)).Clear
Date1 = Date – 1
For Each mi In fold.Items
On Error Resume Next
If mi.Class = olMail And mi.ReceivedTime = Date And mi.SenderEmailAddress = “viveksatpute.gmail.com” Or mi.SenderEmailAddress = “[email protected]” Or mi.SenderEmailAddress = “[email protected]” Then
n = n + 1
Sheet2.Cells(n + 1, 1).Value = mi.ReceivedTime
Sheet2.Cells(n + 1, 2).Value = mi.SenderName & ” ” & mi.SenderEmailAddress
Sheet2.Cells(n + 1, 3).Value = mi.Subject
Sheet2.Cells(n + 1, 4).Value = mi.Body
End If
Next mi
Set olml = Nothing
Set sndr = Nothing
Set fold = Nothing
Sheet2.Range(“A:D”).EntireColumn.AutoFit
Sheet2.Range(“A:D”).EntireColumn.VerticalAlignment = xlTop
Sheet2.Columns(“D:D”).ColumnWidth = 100
End Sub
Hi Sir, Really your videos are awesome!! Way you explain line by line coding helping me in better understanding..
I saw few videos on exporting emails from Inbox or from specific folders under Inbox. Need your help in exporting Subfolders and Sent items as well. Which helps me to get email count actioned by team.
Dear Sir,
It is no doubt that your videos are helpful in many ways to many users. Please do keep sharing new codes on different situations. we would be grateful to you.
In fact I have a quick question. i would like to write a macro that would check the series of email messages (i mean, many threads of responses in one email item) for the earliest date on which the response was sent. FYI – it is about checking one email with many response messages with different SentOn dates and not checking all emails in a folder for earliest SentOn date or the first email in a folder.
Very eagerly looking forward to your reply with a solution.
Thanks a lot.