The given code works successfully. It searches for an email subject in outlook Sent Items folder. The search happens based on a specific date within specific time period. For example, the code below looks for the email title "Test Email Sent on Friday" that was sent on July 20, 2018 between 12:00 AM and 11:59 PM.
In addition to my existing search criteria, how can I filter out emails that were sent out to specific users. I want to check [To] field. If [To] had recipients [email protected], [email protected], or [email protected], then do not return the search results. The search should return "Yes. Email found" if [To] section doesn't have either of these emails: [email protected], [email protected], or [email protected].
Public Function is_email_sent()
Dim olApp As Object
Dim olNs As Object
Dim olFldr As Object
Dim olItms As Object
Dim objItem As Object
On Error Resume Next
Set olApp = CreateObject("Outlook.Application")
Set olNs = olApp.GetNamespace("MAPI")
Set olFldr = olNs.Folders("[email protected]").Folders("Sent Items")
Set olItms = olFldr.Items
Set objItem = olItms.Restrict("[Subject] = ""Test Email Sent on Friday"" And [SentOn] >= ""7/20/2018 12:00 AM"" AND [SentOn] <= ""7/20/2018 11:59 PM""")
If objItem.Count = 0 Then
MsgBox "No. Email not found"
Else
MsgBox "Yes. Email found"
End If
Set olApp = Nothing
Set olNs = Nothing
Set olFldr = Nothing
Set olItms = Nothing
Set objItem = Nothing
End Function