VBA to Reply All To Latest Email Thread
Hello,
I'm trying to build a macro that searches my inbox for a subject line (in this example "REQUEST FOR OVERTIME"), opens the most recent email, and reply all. My macro is a little more nuanced, but borrowed from the example I found online below.
The issue is that it's opening ALL emails with the subject line "REQUEST FOR OVERTIME," not just the most recent. What should I add to this in order to just open the latest message?
Thanks!
Code:
Sub ReplyMail_No_Movements()
Dim olApp As Outlook.Application
Dim olNs As Namespace
Dim Fldr As MAPIFolder
Dim olMail As Variant
Dim i As Integer
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderSentMail)
i = 1
For Each olMail In Fldr.Items
If InStr(olMail.Subject, "REQUEST FOR OVERTIME") <> 0 Then
Set replyall = olMail.replyall
With replyall
StrBody = "Hello " & "<br>" & _
"<p>Following up with the below. May you please advise?" & _
"<p>Thank you," & vbCrLf & vbCrLf & "<br>" & _
"<p>" & Session.CurrentUser.Name
.HTMLBody = StrBody & .HTMLBody
emailReady = True
.Display
i = i + 1
End If
Next olMail
Set olMail = Nothing
Set olApp = Nothing
End Sub
https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
https://www.eileenslounge.com/viewtopic.php?p=312533#p312533
https://www.eileenslounge.com/viewtopic.php?f=44&t=40373&p=312499#p312499
https://www.eileenslounge.com/viewtopic.php?p=311844#p311844
https://archive.org/download/wlsetup-all_201802/wlsetup-all.exe
https://www.eileenslounge.com/viewtopic.php?p=311826#p311826
https://www.eileenslounge.com/viewtopic.php?f=37&t=40261&p=311783#p311783
https://www.eileenslounge.com/viewtopic.php?p=310916#p310916
https://www.eileenslounge.com/viewtopic.php?p=310720#p310720
https://www.eileenslounge.com/viewtopic.php?f=56&t=40034&p=310171#p310171
https://www.eileenslounge.com/viewtopic.php?p=310110#p310110
https://www.eileenslounge.com/viewtopic.php?p=310024#p310024
https://www.eileenslounge.com/viewtopic.php?p=309121#p309121
https://www.eileenslounge.com/viewtopic.php?p=309101#p309101
https://www.eileenslounge.com/viewtopic.php?p=308945#p308945
https://www.eileenslounge.com/viewtopic.php?f=30&t=39858&p=308880#p308880
https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
Need to Search in Other sub folders
Thanks for below Code, but i want to search in sub folders.
Quote:
Originally Posted by
Excel Fox
As long as it working :)
I was modifying my code. For my interest, could you test this and let me know
Code:
Option Explicit
Sub ReplyMail_No_Movements()
Dim olApp As Outlook.Application
Dim olNs As NameSpace
Dim Fldr As MAPIFolder
Dim objMail As Object
Dim objReplyToThisMail As MailItem
Dim lngCount As Long
Dim objConversation As Conversation
Dim objTable As Table
Dim objVar As Variant
Dim strBody As String
Set olApp = Session.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
lngCount = 1
For Each objMail In Fldr.Items
If TypeName(objMail) = "MailItem" Then
If InStr(objMail.Subject, "REQUEST FOR OVERTIME") <> 0 Then 'REQUEST FOR OVERTIME
Set objConversation = objMail.GetConversation
Set objTable = objConversation.GetTable
objVar = objTable.GetArray(objTable.GetRowCount)
Set objReplyToThisMail = olApp.Session.GetItemFromID(objVar(UBound(objVar), 0))
With objReplyToThisMail.ReplyAll
strBody = "Hello " & "<br>" & _
"<p>Following up with the below. May you please advise?" & _
"<p>Thank you," & vbCrLf & vbCrLf & "<br>" & _
"<p>" & Session.CurrentUser.Name
.HTMLBody = strBody & .HTMLBody
.Display
End With
Exit For
End If
End If
Next objMail
Set olApp = Nothing
Set olNs = Nothing
Set Fldr = Nothing
Set objMail = Nothing
Set objReplyToThisMail = Nothing
lngCount = Empty
Set objConversation = Nothing
Set objTable = Nothing
If IsArray(objVar) Then Erase objVar
End Sub
https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=314837#p314837
https://www.eileenslounge.com/viewtopic.php?f=21&t=40701&p=314836#p314836
https://www.eileenslounge.com/viewtopic.php?f=27&t=40621&p=314621#p314621
https://www.eileenslounge.com/viewtopic.php?f=27&t=40621&p=314619#p314619
https://www.eileenslounge.com/viewtopic.php?f=27&t=40621&p=314600#p314600
https://www.eileenslounge.com/viewtopic.php?f=27&t=40621&p=314599#p314599
https://www.eileenslounge.com/viewtopic.php?f=27&t=40621&p=314274#p314274
https://www.eileenslounge.com/viewtopic.php?f=27&t=40621&p=314229#p314229
https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA