View Full Version : Excel vba to search mail in outlook

2017-01-21, 05:41 PM

I have below code to search a mail with specific subject in Outlook

But Below code searched mail only in Inbox. But I want to search mail in all folder and subfolder.

Also I wants to search latest email (sent or received) with specific subject and Wants to do rely all to the same message. Can any one revise the code accordingly?

Sub TestMailTool() ' Is working in Office 2000-2007
Dim OutApp As Object
Dim OutNameSpace As Object
Dim OutFolder As Object
Dim OutItms As Object
Dim OutMail As Object
Dim i As Integer
Dim mail
Dim replyall As Object
'Dim strbody As String
'Dim MyTasks As Object
'Dim sir() As String
'Dim myitems As Outlook.Items
'Dim myitem As Object

Set OutApp = CreateObject("Outlook.Application")
'Set OutMail = OutApp.CreateItem(0)
Set OutNameSpace = OutApp.GetNamespace("MAPI")
Set OutFolder = OutNameSpace.GetDefaultFolder(6)
Set OutItms = OutFolder.Items
i = 1
'Set MyTasks = OutFolder.Items
'Set myitems = myInbox.Items

For Each OutMail In OutFolder.Items
If InStr(OutMail.Subject, "Status of EB Proposal") <> 0 Then

Body = "test reply" & vbCrLf & BR
i = i + 1
End If
Next OutMail
End Sub