1

I came across this code and modified it based on my needs.

I want to reply to the Outlook email I select/highlight.

Instead of replying to the email I select/highlight in Outlook, it replies to the latest email with same subject line of the email I highlighted.

Sub SendEmail()
    Dim OutlookApp As Object
    Dim OutlookMail As Object
   
    Set OutlookApp = CreateObject("Outlook.Application")
    Set OutlookMail = OutlookApp.ActiveExplorer.Selection.Item(1)
   
    Dim OutlookConversation As Object
    Set OutlookConversation = OutlookMail.GetConversation
   
    Dim OutlookTable As Object
    Set OutlookTable = OutlookConversation.GetTable
   
    Dim OutlookAr As Variant
    OutlookAr = OutlookTable.GetArray(OutlookTable.GetRowCount)
   
    Dim OutlookReplyToThisMail As Object
    Set OutlookReplyToThisMail = OutlookMail.Session.GetItemFromID(OutlookAr(UBound(OutlookAr), 0))
   
    With OutlookReplyToThisMail.ReplyAll
        .Subject = Sheet1.Range("O2") & "_" & .Subject
        .HTMLBody = "<p style='font-family:calibri;font-size:13'>" & _
        Sheet1.Range("D25") & "<br>" & "<br>" & _
        Sheet1.Range("D26") & "<br>" & "<br>" & _
        Sheet1.Range("D27") & Signature & .HTMLBody
        .Display
    End With
End Sub
Community
  • 1
  • 1
JP0710
  • 41
  • 6
  • 2
    Do you mean that you select a mail and want replaying to it? If so, use `With OutlookMail.ReplyAll` instead of `With OutlookReplyToThisMai.ReplyAll` and comment all code lines between `Set OutlookMail = OutlookApp.ActiveExplorer.Selection.Item(1)` and the above recommended line to be changed... Your code extract the last mail of a **specific conversation**... – FaneDuru Jan 03 '23 at 11:35
  • https://stackoverflow.com/a/31818677/4539709 – 0m3r Jan 03 '23 at 18:02

1 Answers1

0

However, instead of replying to the specific email which i selected/highlighted in outlook, it replies to the latest version (latest email with same subject line) of the email which i highlighted.

There is no need to use the Conversation object in the code:

    Dim OutlookConversation As Object
    Set OutlookConversation = OutlookMail.GetConversation
   
    Dim OutlookTable As Object
    Set OutlookTable = OutlookConversation.GetTable
   
    Dim OutlookAr As Variant
    OutlookAr = OutlookTable.GetArray(OutlookTable.GetRowCount)
   
    Dim OutlookReplyToThisMail As Object
    Set OutlookReplyToThisMail = OutlookMail.Session.GetItemFromID(OutlookAr(UBound(OutlookAr), 0))

Instead, you need to get the selected items in the following way:

Set myOlExp = Application.ActiveExplorer  
Set myOlSel = myOlExp.Selection  
For x = 1 To myOlSel.Count  
  If myOlSel.Item(x).Class = OlObjectClass.olMail Then  
    ' For mail item, use the SenderName property.  
    
     Dim OutlookReplyToThisMail As Outlook.MailItem
    Set OutlookReplyToThisMail = myOlSel.Item(x)  
   
    With OutlookReplyToThisMail.ReplyAll
        .Subject = Sheet1.Range("O2") & "_" & .Subject
        .HTMLBody = "<p style='font-family:calibri;font-size:13'>" & _
        Sheet1.Range("D25") & "<br>" & "<br>" & _
        Sheet1.Range("D26") & "<br>" & "<br>" & _
        Sheet1.Range("D27") & Signature & .HTMLBody
        .Display
       
    End With
  EndIf 
Next

The Explorer.Selection property returns a Selection object that contains the item or items that are selected in the explorer window. If the current folder displays a folder home page, this property returns an empty collection. Also, if a group header such as Today, or a conversation group header is selected, the Count property on the returned Selection object is zero.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45