PDA

View Full Version : Macro to Search Specific Subject, find, & reply all - Outlook 2016



Toledo
06-17-2020, 07:44 AM
Title says it all.


Needing to have a macro where it can search the inbox, sent, draft, & outbox, for the latest message containing a particular Subject line & reply all to (Trying to continue the threads of specific emails)


How can I do this?
Thank you in advance to all the resident Geniuses! :D

gmayor
06-17-2020, 09:02 PM
Searching the inbox and its sub folders is not that difficult, but I fail to see the point of searching sent, draft, & outbox; and replies to sent or outbox messages would go to you.

The following should get you started


Option Explicit
'Graham Mayor - https://www.gmayor.com - Last updated - 18 Jun 2020
Private Const sSubject As String = '"The text to find"

Sub ProcessFolders()
Dim cFolders As Collection
Dim olFolder As Outlook.Folder
Dim SubFolder As Folder
Dim olNS As Outlook.NameSpace
On Error Resume Next
Set cFolders = New Collection
Set olNS = GetNamespace("MAPI")
cFolders.Add olNS.PickFolder
Do While cFolders.Count > 0
Set olFolder = cFolders(1)
cFolders.Remove 1
FindSubject olFolder
For Each SubFolder In olFolder.folders
cFolders.Add SubFolder
Next SubFolder
Loop
lbl_Exit:
Set olFolder = Nothing
Set SubFolder = Nothing
Exit Sub
err_Handler:
GoTo lbl_Exit
End Sub

Private Sub FindSubject(iFolder As Folder)
Dim olItem As Outlook.MailItem
Dim olMsg As Outlook.MailItem
Dim i As Long
iFolder.items.Sort "[Received]", True
For i = iFolder.items.Count To 1 Step -1
Set olItem = iFolder.items(i)
If TypeName(olItem) = "MailItem" Then
If InStr(1, olItem.Subject, sSubject) > 0 Then
Debug.Print olItem.Subject & vbTab & olItem.sender
Set olMsg = olItem.ReplyAll
olMsg.Display
'do stuff with olMsg
Exit For
End If
End If
Next i
lbl_Exit:
Set olItem = Nothing
Exit Sub
End Sub

Toledo
06-18-2020, 07:05 AM
I'll give this a try, thank you!
My reason behind needing this is because I generate allot of emails that require replying to the very latest reply within the thread.
Sometimes, I have a draft already in progress, or an email is stuck in my outbox, or was already sent.
Does this help?