How to Search Outlook mails with in the inbox and sub folders - excel

I have created a macro which takes the latest mail and send the reply all.
Now how do I search Inbox and sub folders and pick the latest one.
My code picks the mail only from Inbox.
Option Explicit
Public Sub TESTRUN()
Dim olApp As Outlook.Application
Set olApp = New Outlook.Application
Dim olNs As Outlook.Namespace
Set olNs = olApp.GetNamespace("MAPI")
Dim Inbox As Outlook.MAPIFolder
Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
Dim Subject As String
Subject = ThisWorkbook.Sheets("SendMail").Range("B5").Text
Debug.Print Subject
Dim fpath As String
fpath = ThisWorkbook.Sheets("SendMail").Range("A8").Value
Dim i As Long
Dim Filter As String
Filter = "#SQL=" & Chr(34) & "urn:schemas:httpmail:datereceived" & _
Chr(34) & " >= '01/01/1900' And " & _
Chr(34) & "urn:schemas:httpmail:datereceived" & _
Chr(34) & " < '12/31/2100' And " & _
Chr(34) & "urn:schemas:httpmail:subject" & _
Chr(34) & "Like '%" & Subject & "%'"
Dim Items As Outlook.Items
Set Items = Inbox.Items.Restrict(Filter)
Items.Sort "[ReceivedTime]", False
For i = Items.Count To 1 Step -1
DoEvents
If TypeOf Items(i) Is MailItem Then
Dim Item As Object
Set Item = Items(i)
Debug.Print Item.Subject ' Print on Immediate Window
Debug.Print Item.ReceivedTime ' Print on Immediate Window
Dim ReplyAll As Outlook.MailItem
Set ReplyAll = Item.ReplyAll
With ReplyAll
.Subject = Left(ActiveWorkbook.Name, InStr(ActiveWorkbook.Name, ".") - 1)
.HTMLBody = "<font size=""3"" face=""Calibri"">" & _
"Hi Veronica, <br><br>" & _
"The " & Left(ActiveWorkbook.Name, _
InStr(ActiveWorkbook.Name, ".") - 1) & _
"</B> has been prepared and ready for your review.<br>" & _
"</B> <br>" & _
"" & fpath & "" & .HTMLBody
.Display
Exit For
End With
End If
Next
End Sub

You could convert your code recursive function start from Inbox :Example
Option Explicit
Public Sub Example()
Dim olNs As Outlook.NameSpace
Dim Inbox As Outlook.MAPIFolder
Set olNs = Application.GetNamespace("MAPI")
Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
' // Process Current Folder
LoopFolders Inbox
Set Inbox = Nothing
End Sub
Private Function LoopFolders(ByVal ParentFldr As Outlook.MAPIFolder)
Dim Subject As String
Subject = ThisWorkbook.Sheets("SendMail").Range("B5").Text
Dim FPath As String
FPath = ThisWorkbook.Sheets("SendMail").Range("A8").Value
Dim Filter As String
Filter = "#SQL=" & Chr(34) & "urn:schemas:httpmail:datereceived" & _
Chr(34) & " >= '01/01/1900' And " & _
Chr(34) & "urn:schemas:httpmail:datereceived" & _
Chr(34) & " < '12/31/2100' And " & _
Chr(34) & "urn:schemas:httpmail:subject" & _
Chr(34) & "Like '%" & Subject & "%'"
Dim Items As Outlook.Items
Set Items = ParentFldr.Items.Restrict(Filter)
Items.Sort "[ReceivedTime]", False
Dim i As Long
For i = Items.Count To 1 Step -1
DoEvents
If TypeOf Items(i) Is MailItem Then
Dim Item As Object
Set Item = Items(i)
Debug.Print Item.Subject & " " & Item.ReceivedTime
Dim ReplyAll As Outlook.MailItem
Set ReplyAll = Item.ReplyAll
With ReplyAll
.Subject = ""
.HTMLBody = "" '
.Display
End With
Exit Function
End If
Next
Dim SubFldr As Outlook.MAPIFolder
' // Recurse through SubFldrs
If ParentFldr.Folders.Count > 0 Then
For Each SubFldr In ParentFldr.Folders
LoopFolders SubFldr
Debug.Print SubFldr.Name
Next
End If
End Function

Related

Code not executing because the object doesn't support the filter property

I am not able to filter the selected mail item by Subject.
The issue is that
If TypeOf Items(1) Is Outlook.MailItem And Items(1).Restrict(sFilter) Then
is triggering the error
Run time error 438: Object doesn't support this property or method.
Steps:
Loop through cells for different subject names
Search the inbox and Sentitem folders for the latest email for selected "Subject" as sometimes people do not respond to your email. So latest email is in the sent items and not in your inbox
select the latest email and reply to all
For the body of the email, I am running another function to get the required info.
The code:
Sub AccessInbox6()
'Early binding
Dim Olook As Outlook.Application ' to access all the libraries of outlook
Set Olook = New Outlook.Application
Dim sFilter As String
Dim sSubject As String
' Restrict items and running the loop
Sheet1.Range("A2").Select
Do Until ActiveCell.Value = "" 'Using this to loop over multiple cells containing subjects
sSubject = ActiveCell.Value
sFilter = "[Subject] = '" & sSubject & "'"
Dim Items As Outlook.Items
Set Items = Olook.GetNamespace("MAPI") _
.GetDefaultFolder(olFolderInbox).Items 'Checking the inbox
Dim Items2 As Outlook.Items
Set Items2 = Olook.GetNamespace("MAPI") _
.GetDefaultFolder(olFolderSentMail).Items 'Checking the sent items
Items.Sort "ReceivedTime", True 'to put them in order by date
Items2.Sort "ReceivedTime", True 'to put them in order by date or I should use "SentOn"
'Items2.Sort "SentOn", True
If Items.Item(1).ReceivedTime > Items2.Item(1).ReceivedTime Then 'Here I am checking which email is latest by date either in inbox or SentItems
If TypeOf Items(1) Is Outlook.MailItem And Items(1).Restrict(sFilter) Then 'Getting error here - Here I am checking if the "Subject of the email matches with what I have in the excel sheet
Debug.Print Items(1).Subject ' Print on Immediate Window
With Items(1).ReplyAll
.Display
.Body = "Dear Someone" & vbNewLine & vbNewLine & GetPSMUpdate2 & vbNewLine & vbNewLine & "Sincerely," & vbNewLine & "XX-"
'.Attachments.Add Environ("UserProfile") & "\Desktop\Tracking Sheet.xlsx"
.To = "XXX#gmail.com"
.Subject = "PSM Report"
'.Send
End With
Else
MsgBox "No emails found with Subject:" & vbLf & "'" & sSubject & "'"
End If
Else
If TypeOf Items2(1) Is Outlook.MailItem And Items2(1).Restrict(sFilter) Then
Debug.Print Items(1).Subject ' Print on Immediate Window
With Items(1).ReplyAll
.Display
.Body = "Dear Someone" & vbNewLine & vbNewLine & GetPSMUpdate2 & vbNewLine & vbNewLine & "Sincerely," & vbNewLine & "XX-"
'.Attachments.Add Environ("UserProfile") & "\Desktop\Tracking Sheet.xlsx"
.To = "XXX#gmail.com"
.Subject = "PSM Report"
'.Send
End With
Else
MsgBox "No emails found with Subject:" & vbLf & "'" & sSubject & "'"
End If
End If
ActiveCell.Offset(1, 0).Select
Loop
End Sub
Function GetPSMUpdate2() As String
Dim PSMColumn As Range, PSMRow As Range, r As Range, C As Range
Dim Str As String
Sheet2.Activate
Set PSMColumn = Range("A2", Range("A1").End(xlDown))
For Each r In PSMColumn
Set PSMRow = Range(r, r.End(xlToRight))
For Each C In PSMRow
Str = Str & C.Value
If C.Column < r.End(xlToRight).Column Then
Str = Str & vbTab
End If
Next C
If r.Row < Range("A1").End(xlDown).Row Then
Str = Str & vbNewLine
End If
Next r
GetPSMUpdate2 = Str
End Function
Use of Item in the variable names causes some confusion as well the filter could be separated.
Option Explicit
Sub AccessInbox6Fix()
'Early binding
Dim Olook As outlook.Application
Dim ItemsRaw As outlook.Items
Dim Items2Raw As outlook.Items
Dim Items As outlook.Items
Dim Items2 As outlook.Items
Dim sFilter As String
Dim sSubject As String
Set Olook = New outlook.Application
Sheet1.Range("A2").Select
Do Until ActiveCell.Value = "" ' Loop over cells containing subjects
'Checking the inbox
Set ItemsRaw = Olook.Session.GetDefaultFolder(olFolderInbox).Items
Debug.Print "Raw counts"
Debug.Print " ItemsRaw.Count: " & ItemsRaw.Count
'Checking the sent items
Set Items2Raw = Olook.Session.GetDefaultFolder(olFolderSentMail).Items
Debug.Print " Items2Raw.Count: " & Items2Raw.Count
sSubject = ActiveCell.Value
sFilter = "[Subject] = '" & sSubject & "'"
Debug.Print
Debug.Print sFilter
Debug.Print "Subject counts"
Set Items = ItemsRaw.Restrict(sFilter)
Debug.Print " Items.Count: " & Items.Count
Set Items2 = Items2Raw.Restrict(sFilter)
Debug.Print " Items2.Count: " & Items2.Count
Items.Sort "SentOn", True
Items2.Sort "SentOn", True
If Items.Item(1).ReceivedTime > Items2.Item(1).ReceivedTime Then
If TypeOf Items.Item(1) Is MailItem Then
Debug.Print Items.Item(1).Subject
With Items.Item(1).ReplyAll
.Display
.To = "XXX#noplacenowhere.com"
.Subject = "PSM Report"
'.Send
End With
Else
MsgBox "Most recent item is not a mailitem:" & vbLf & "'" & sSubject & "'"
End If
Else
If TypeOf Items2.Item(1) Is outlook.MailItem Then
Debug.Print Items2.Item(1).Subject ' Print on Immediate Window
With Items2.Item(1).ReplyAll
.Display
.To = "XXX#noplacenowhere.com"
.Subject = "PSM Report"
'.Send
End With
Else
MsgBox "Most recent item is not a mailitem:" & vbLf & "'" & sSubject & "'"
End If
End If
ActiveCell.Offset(1, 0).Select
Loop
Debug.Print "Done."
End Sub

Loop through all Outlook inboxes including shared inboxes error

I have code that will search through a user's Outlook and reply to an email depending on the Subject phrase you input in the worksheet cell. I did have it working a couple days ago, but now I can not seem to get it to work (was deleted). Once run, an error message will continuously display for code line "Set olitems = flrd.Items", saying "Object variable or With block variable not set". I think the problem is the End if but wherever I place it either the code does nothing or the same error displays.
The only other problem with the working code (when it worked) was that it populated more than once. I wish it to populate only one time.
Sub Display()
Dim Fldr As Outlook.Folder
Dim olfolder As Outlook.MAPIFolder
Dim olMail As Outlook.MailItem
Dim olReply As Outlook.MailItem
Dim olItems As Outlook.Items
Dim i As Integer
Dim signature As String
Dim allStores As Stores
Dim storeInbox As Folder
Dim j As Long
Set allStores = Session.Stores
For j = 1 To allStores.count
On Error Resume Next
Debug.Print i & " DisplayName - " & allStores(i).DisplayName
On Error GoTo 0
Set storeInbox = Nothing
On Error Resume Next
Set storeInbox = allStores(i).GetDefaultFolder(olFolderInbox)
On Error GoTo 0
If Not storeInbox Is Nothing Then
End if
Set olItems = Fldr.Items
olItems.Sort "[Received]", True
For i = 1 To olItems.count
signature = Environ("appdata") & "\Microsoft\Signatures\"
If Dir(signature, vbDirectory) <> vbNullString Then
signature = signature & Dir$(signature & "*.htm")
Else
signature = ""
End If
signature = CreateObject("Scripting.FileSystemObject").GetFile(signature).OpenAsTextStream(1, -2).ReadAll
Set olMail = olItems(i)
If InStr(olMail.Subject, Worksheets("Checklist Form").Range("B8")) <> 0 Then
If Not olMail.Categories = "Executed" Then
Set olReply = olMail.ReplyAll
With olReply
.HTMLBody = "<p style='font-family:calibri;font-size:14.5'>" & "Hi Everyone," & _
"<p style='font-family:calibri;font-size:14.5'>" & "Workflow ID:" & " " & _
Worksheets("Checklist Form").Range("B6") & "<p style='font-family:calibri;font-size:14.5'>" & _
Worksheets("Checklist Form").Range("B11") & "<p style='font-family:calibri;font-size:14.5'>" & _
"Regards," & "</p><br>" & signature & .HTMLBody
.Display
.Subject = "RO Finalized WF:" & Worksheets("Checklist Form").Range("B6") & " " & _
Worksheets("Checklist Form").Range("B2") & " -" & Worksheets("Fulfillment Checklist").Range("B3")
End With
Exit For
olMail.Categories = "Executed"
End If
End If
Next i
Set Fldr = StoreInbox
Next
ExitRoutine:
Set allStores = Nothing
Set storeInbox = Nothing
End Sub
Firstly, get rid of the On Error Resume Next line.
Secondly, Fldr variable is never set. Did you mean to use storeInbox variable instead?
Sub Display()
Dim Fldr As Outlook.Folder
Dim olfolder As Outlook.MAPIFolder
Dim olMail As Outlook.MailItem
Dim olReply As Outlook.MailItem
Dim olItems As Outlook.Items
Dim i As Integer
Dim signature As String
Dim allStores As Stores
Dim storeInbox As Folder
Dim j As Long
Set allStores = Session.Stores
For j = 1 To allStores.count
On Error Resume Next
Debug.Print i & " DisplayName - " & allStores(i).DisplayName
On Error GoTo 0
Set storeInbox = Nothing
On Error Resume Next
Set storeInbox = allStores(i).GetDefaultFolder(olFolderInbox)
On Error GoTo 0
If Not storeInbox Is Nothing Then
Set olItems = Fldr.Items
olItems.Sort "[Received]", True
For i = 1 To olItems.count
signature = Environ("appdata") & "\Microsoft\Signatures\"
If Dir(signature, vbDirectory) <> vbNullString Then
signature = signature & Dir$(signature & "*.htm")
Else
signature = ""
End If
signature = CreateObject("Scripting.FileSystemObject").GetFile(signature).OpenAsTextStream(1, -2).ReadAll
Set olMail = olItems(i)
If InStr(olMail.Subject, Worksheets("Checklist Form").Range("B8")) <> 0 Then
If Not olMail.Categories = "Executed" Then
Set olReply = olMail.ReplyAll
With olReply
.HTMLBody = "<p style='font-family:calibri;font-size:14.5'>" & "Hi Everyone," & _
"<p style='font-family:calibri;font-size:14.5'>" & "Workflow ID:" & " " & _
Worksheets("Checklist Form").Range("B6") & "<p style='font-family:calibri;font-size:14.5'>" & _
Worksheets("Checklist Form").Range("B11") & "<p style='font-family:calibri;font-size:14.5'>" & _
"Regards," & "</p><br>" & signature & .HTMLBody
.Display
.Subject = "RO Finalized WF:" & Worksheets("Checklist Form").Range("B6") & " " & _
Worksheets("Checklist Form").Range("B2") & " -" & Worksheets("Fulfillment Checklist").Range("B3")
End With
Exit For
olMail.Categories = "Executed"
End If
End If
Next i
End if
Next
ExitRoutine:
Set allStores = Nothing
Set storeInbox = Nothing
End Sub

VBA to loop through all inboxes including shared inboxes

I have working code that replies to an email in the user's Outlook, based on the subject. However I am not able to have the code search through all the user's inboxes.
As of now it will only search through the user's specific inbox. Here is my code, I have searched around but I can not find a solution that my knowledge of VBA can comprehend.
Sub Display()
Dim Fldr As Outlook.Folder
Dim olfolder As Outlook.MAPIFolder
Dim olMail As Outlook.MailItem
Dim olReply As Outlook.MailItem
Dim olItems As Outlook.Items
Dim i As Integer
Dim signature As String
Set Fldr = Session.GetDefaultFolder(olFolderInbox)
Set olItems = Fldr.Items
olItems.Sort "[Received]", True
For i = 1 To olItems.count
signature = Environ("appdata") & "\Microsoft\Signatures\"
If Dir(signature, vbDirectory) <> vbNullString Then
signature = signature & Dir$(signature & "*.htm")
Else
signature = ""
End If
signature = CreateObject("Scripting.FileSystemObject").GetFile(signature).OpenAsTextStream(1, -2).ReadAll
Set olMail = olItems(i)
If InStr(olMail.Subject, Worksheets("Checklist Form").Range("B8")) <> 0 Then
If Not olMail.Categories = "Executed" Then
Set olReply = olMail.ReplyAll
With olReply
.HTMLBody = "<p style='font-family:calibri;font-size:14.5'>" & "Hi Everyone," & _
"<p style='font-family:calibri;font-size:14.5'>" & "Workflow ID:" & " " & _
Worksheets("Checklist Form").Range("B6") & "<p style='font-family:calibri;font-size:14.5'>" & _
Worksheets("Checklist Form").Range("B11") & "<p style='font-family:calibri;font-size:14.5'>" & _
"Regards," & "</p><br>" & signature & .HTMLBody
.Display
.Subject = "RO Finalized WF:" & Worksheets("Checklist Form").Range("B6") & " " & _
Worksheets("Checklist Form").Range("B2") & " -" & Worksheets("Fulfillment Checklist").Range("B3")
End With
Exit For
olMail.Categories = "Executed"
End If
End If
Next i
End Sub
You may reference any Inbox like this:
Option Explicit
Sub Inbox_by_Store()
Dim allStores As Stores
Dim storeInbox As Folder
Dim j As Long
Set allStores = Session.Stores
For j = 1 To allStores.count
Debug.Print j & " DisplayName - " & allStores(j).DisplayName
Set storeInbox = Nothing
' Some stores will not have an inbox
' Bypass possible expected error if there is no inbox in the store
On Error Resume Next
' Note this is one of the rare acceptable uses for On Error Resume Next
Set storeInbox = allStores(j).GetDefaultFolder(olFolderInbox)
' Turn off error bypass as soon as it is no longer needed
On Error GoTo 0
If Not storeInbox Is Nothing Then
storeInbox.Display
' your code here instead of storeInbox.Display
' Set Fldr = storeInbox
End If
Next
ExitRoutine:
Set allStores = Nothing
Set storeInbox = Nothing
End Sub
I don't really have the ability to test out whether this works, but these are the changes that I mentioned in the comments, I hope they work!
Sub Display()
'...
Set Fldr = Session.GetDefaultFolder(olFolderInbox)
Dim mySubfolder As Outlook.Folder 'added
For Each mySubfolder In Fldr.Folders 'added
Set olItems = mySubfolder.Items 'changed
For i = 1 To olItems.count
'...
Next i
Next mySubfolder 'added
End Sub

Copy range of cells from Excel as picture and add text in the email body

I'm tying to add range of cells as a picture from the active workbook along with some text.
But for some reason it skipping the text and only pasting the image in the email body.
How do I fix this?
Option Explicit
Public Sub POSTRUN()
Dim olApp As Outlook.Application
Set olApp = New Outlook.Application
Dim Olobj As Outlook.Application
Set Olobj = CreateObject("Outlook.Application")
Dim olNs As Outlook.Namespace
Set olNs = olApp.GetNamespace("MAPI")
Dim Inbox As Outlook.MAPIFolder
Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
Dim subject As String
subject = ThisWorkbook.Sheets("SendMail").Range("I5").Text
Debug.Print subject
Dim i As Long
Dim Filter As String
Filter = "#SQL=" & Chr(34) & "urn:schemas:httpmail:datereceived" & _
Chr(34) & " >= '01/01/1900' And " & _
Chr(34) & "urn:schemas:httpmail:datereceived" & _
Chr(34) & " < '12/31/2100' And " & _
Chr(34) & "urn:schemas:httpmail:subject" & _
Chr(34) & "Like '%" & subject & "%'"
Dim Items As Outlook.Items
Set Items = Inbox.Items.Restrict(Filter)
Items.Sort "[ReceivedTime]", False
For i = Items.Count To 1 Step -1
DoEvents
If TypeOf Items(i) Is MailItem Then
Dim Item As Object
Set Item = Items(i)
Debug.Print Item.subject ' Print on Immediate Window
Debug.Print Item.ReceivedTime ' Print on Immediate Window
Dim r As Range
Set r = ThisWorkbook.Sheets("post").Range("A1:M30")
r.Copy
Dim outMail As Outlook.MailItem
Set outMail = Olobj.CreateItem(olMailItem)
Dim body
Dim ReplyAll As Outlook.MailItem
Set ReplyAll = Item.ReplyAll
Dim wordDoc As Word.Document
Set wordDoc = ReplyAll.GetInspector.WordEditor
With ReplyAll
.HTMLBody = "<font size=""3"" face=""Calibri"">" & _
"Hi <br><br>" & _
"The " & Left(ActiveWorkbook.Name, _
InStr(ActiveWorkbook.Name, ".") - 1) & _
"</B> has been posted.<br>" & _
.HTMLBody
wordDoc.Range.PasteAndFormat wdChartPicture
.Display
Exit For
End With
End If
Next
End Sub
Its not skipping, you are simply overriding the HTMLBody with the image your pasting, so what you need to do is work with Paragraphs Object (Word)
Example
With ReplyAll
.HTMLBody = "<font size=""3"" face=""Calibri"">" & _
"Hi <br><br>" & _
"The " & Left(ActiveWorkbook.Name, _
InStr(ActiveWorkbook.Name, ".") - 1) & _
"</B> has been posted.<br>" & .HTMLBody
.Display
With wordDoc.Paragraphs(2)
.Range.InsertParagraphAfter
.Range.PasteAndFormat Type:=wdChartPicture
.Range.ParagraphFormat.LineSpacingRule = wdLineSpaceDouble
End With
Exit For
End With
Also remove following code
Dim Olobj As Outlook.Application
Set Olobj = CreateObject("Outlook.Application")
Dim outMail As Outlook.MailItem
Set outMail = Olobj.CreateItem(olMailItem)
Dim body
You already have it
Dim olApp As Outlook.Application
Set olApp = New Outlook.Application
Dim Item As Object
Set Item = Items(i)

Replying to an Outlook mail based on subject, received time and date

I will receive the Journal entries from the client and need to reply to the same mail.
I have tried the below code to execute reply to chain of mails. But the code is returning the all mails which contains the same subject line.
I need to reply which comes recently. I have tried many solutions, but I unable to fix it.
How do I Reply to an email based on subject, received time and date
Sub mail()
Dim olApp As Outlook.Application
Dim olNs As Namespace
Dim Fldr As MAPIFolder
Dim olFldr As MAPIFolder
Dim olMail ' As Outlook.MailItem
Dim i As Integer
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
Set olFldr = Fldr
For Each olMail In olFldr.Items
If InStr(olMail.Subject, Range("C2")) <> 0 Then
Set ReplyAll = olMail.ReplyAll
With ReplyAll
.HTMLBody = "<font size=""3"" face=""Calibri"">" & _
"Hi Veronica <br><br>" & _
"The " & _
Left(ActiveWorkbook.Name, InStr(ActiveWorkbook.Name, ".") - 1) & "</B> has been posted.<br>" & _
"<br><br>Regards," & _
"<br><br>Rajesh</font>" & .HTMLBody
emailReady = True
.Display
End With
End If
Next olMail
If Not emailReady Then
i = i + 1
If i > Fldr.Folders.Count Then
MsgBox ("The email with the given subject line was not found!")
Exit Sub
Else
Set olFldr = Fldr.Folders(i)
GoTo tryAgain
End If
End If
End Sub
Work with Items.Restrict Method (Outlook) to Filter by Subject and Date & Time
Make sure to convert your subject Range("C2") to string variable then use it on your filter
Example
Option Explicit
Public Sub Example()
Dim olApp As Outlook.Application
Set olApp = New Outlook.Application
Dim olNs As Outlook.Namespace
Set olNs = olApp.GetNamespace("MAPI")
Dim Inbox As Outlook.MAPIFolder
Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
Dim Subject As String
Subject = ThisWorkbook.Sheets("Sheet1").Range("C2").Text
Debug.Print Subject
Dim i As Long
Dim Filter As String
Filter = "#SQL=" & Chr(34) & "urn:schemas:httpmail:datereceived" & _
Chr(34) & " >= '02/20/2018' And " & _
Chr(34) & "urn:schemas:httpmail:datereceived" & _
Chr(34) & " < '02/25/2018' And " & _
Chr(34) & "urn:schemas:httpmail:subject" & _
Chr(34) & "Like '%" & Subject & "%'"
Dim Items As Outlook.Items
Set Items = Inbox.Items.Restrict(Filter)
Items.Sort "[ReceivedTime]"
For i = Items.Count To 1 Step -1
DoEvents
If TypeOf Items(i) Is mailitem Then
Dim Item As Object
Set Item = Items(i)
Debug.Print Item.Subject ' Print on Immediate Window
Debug.Print Item.ReceivedTime ' Print on Immediate Window
End If
Next
End Sub
Items.Restrict Method Applies a filter to the Items collection, returning a new collection containing all of the items from the original that match the filter.
Edit - Complete Code
Option Explicit
Public Sub Example()
Dim olApp As Outlook.Application
Set olApp = New Outlook.Application
Dim olNs As Outlook.Namespace
Set olNs = olApp.GetNamespace("MAPI")
Dim Inbox As Outlook.MAPIFolder
Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
Dim Subject As String
Subject = ThisWorkbook.Sheets("Sheet1").Range("C2").Text
Debug.Print Subject
Dim i As Long
Dim Filter As String
Filter = "#SQL=" & Chr(34) & "urn:schemas:httpmail:datereceived" & _
Chr(34) & " >= '03/07/2018' And " & _
Chr(34) & "urn:schemas:httpmail:datereceived" & _
Chr(34) & " < '03/25/2018' And " & _
Chr(34) & "urn:schemas:httpmail:subject" & _
Chr(34) & "Like '%" & Subject & "%'"
Dim Items As Outlook.Items
Set Items = Inbox.Items.Restrict(Filter)
Items.Sort "[ReceivedTime]"
For i = Items.Count To 1 Step -1
DoEvents
If TypeOf Items(i) Is MailItem Then
Dim Item As Object
Set Item = Items(i)
Debug.Print Item.Subject ' Print on Immediate Window
Debug.Print Item.ReceivedTime ' Print on Immediate Window
Dim ReplyAll As Outlook.MailItem
Set ReplyAll = Item.ReplyAll
With ReplyAll
.HTMLBody = "<font size=""3"" face=""Calibri"">" & _
"Hi Veronica <br><br>" & _
"The " & Left(ActiveWorkbook.name, _
InStr(ActiveWorkbook.name, ".") - 1) & _
"</B> has been posted.<br>" & _
"<br><br>Regards," & _
"<br><br>Rajesh</font>" & .HTMLBody
.Display
End With
End If
Next
End Sub
https://stackoverflow.com/a/43622710/4539709

Resources