I've come across several email filtering solution but couldn't solve my problem using those ideas.
How do I compare two Outlook folders and based on the first folder filter out the emails not in the second folder according to SentOn
or ReceiveTime
and copy those mails to the second folder?
I can get the mails in both folders using If sMail.SentOn = dMail.SentOn
.
If I alter the condition to If sMail.SentOn <> dMail.SentOn
it is not working.
Sub FindMails()
Dim olApp As Outlook.Application
Dim olNS As NameSpace
Dim olFolder As Folder
Dim olFolder2 As Folder
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olFolder = olNS.Folders.Item("hayat_archive_Loc").Folders.Item("Inbox")
Set olFolder2 = olNS.Folders.Item("Xhayat_archive_Loc").Folders.Item("Inbox")
For Each sMail In olFolder.Items
For Each dMail In olFolder2.Items
If sMail.SentOn <> dMail.SentOn Then
Debug.Print sMail.SentOn & vbTab & sMail.Subject
End If
Next
Next
End Sub
Debug Output If sMail.SentOn = dMail.SentOn
9/9/2017 12:27:34 PM Access Problem
9/9/2017 9:07:33 AM Report on 08-Sep-2017.
9/9/2017 6:39:51 AM Handover of 08th September, 2017
Debug Output If sMail.SentOn <> dMail.SentOn
9/9/2017 12:38:36 PM Access Problem
9/9/2017 12:38:36 PM Access Problem
9/9/2017 12:38:36 PM Access Problem
9/9/2017 12:27:34 PM Access Problem
9/9/2017 12:27:34 PM Access Problem
9/9/2017 9:10:13 AM Egress (09-September-2017)
9/9/2017 9:10:13 AM Egress (09-September-2017)
9/9/2017 9:10:13 AM Egress (09-September-2017)
9/9/2017 9:07:33 AM Report on 08-Sep-2017.
9/9/2017 9:07:33 AM Report on 08-Sep-2017.
9/9/2017 7:23:41 AM Password reset
9/9/2017 7:23:41 AM Password reset
9/9/2017 7:23:41 AM Password reset
9/9/2017 7:04:55 AM Report on 08-Sep-2017.
9/9/2017 7:04:55 AM Report on 08-Sep-2017.
9/9/2017 7:04:55 AM Report on 08-Sep-2017.
9/9/2017 6:39:51 AM Handover of 08th September, 2017
9/9/2017 6:39:51 AM Handover of 08th September, 2017
9/9/2017 2:45:18 AM Usages report on 07th September , 2017
9/9/2017 2:45:18 AM Usages report on 07th September , 2017
9/9/2017 2:45:18 AM Usages report on 07th September , 2017
Restul
andMsgBox
as you have. I would replaceRestul = Restul & sMail.Subject & vbCrLf
withDebug.Print sMail.Subject
.Debug.Print
outputs to the Immediate Window which you can study at your leisure or use Copy & Paste to transfer its contents elsewhere. You cannot do either withMsgBox
. The Immediate Window will accept 200 or so lines before the oldest lines are lost. If 200 is not enough I will show you how to output to a file. – Tony DallimoreIf sMail.SentOn <> dMail.SentOn
was not working,Debug.Print
return same as msgbox in this case. Which is, it suppose to return emails those are not sameSentOn
in both folder, But rather it return each emails multiple times . – Hayat Hasan