skip to Main Content

I have the following code. My objective is to basically create a replier in VBA that simulates exactly what would happen if user clicked "Reply all" on outlook. It works almost perfectly but I the images from the signatures become errors in the reply email. Here’s the code:

Sub LookForEmail(EmailSubject As String)

    Dim objNS As outlook.Namespace: Set objNS = GetNamespace("MAPI")
    Dim olFolder As outlook.MAPIFolder
    Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
    Dim Item As Object
    Dim NewMail As MailItem
    Dim MailFolder As outlook.Items
    Dim sFilter As String
    Dim Counter As Integer
    
    sFilter = "@SQL=""http://schemas.microsoft.com/mapi/proptag/0x0037001f"" like '%" & EmailSubject & "%'"
    Set MailFolder = olFolder.Items.Restrict(sFilter)
    MailFolder.Sort "ReceivedTime", True
    
    For Each Item In MailFolder
        If TypeOf Item Is outlook.MailItem Then
            Dim oMail As outlook.MailItem: Set oMail = Item
            If oMail.Subject = EmailSubject Then
                
                Set NewMail = outlook.CreateItem(olMailItem)

                With NewMail
                    .Display
                    .HTMLBody = "<HTML><Body><span>test</span><Body></HTML>" _
                    & "<span>" & "test2" & "</span>" _
                    & oMail.ReplyAll.HTMLBody
                    .To = oMail.SenderEmailAddress
                    .cc = oMail.cc
                    .BCC = oMail.BCC
                    .Subject = oMail.Subject 'add here the code
                    .Display
                End With
                
                Exit For
                
            End If
        End If
    Next
    
    
End Sub

I’m guessing it’s a problem with the HTML format but I’m not much of an expert on that. Thank you in advance.

I tried creating a new email object in order to include all the details I wanted to but the siganure is still off.

2

Answers


  1. You cannot concatenate two HTML strings and expect a valid HTML, the two must be merged. Read the existing HTMLBody property, find the position of the <body> tag, and insert your HTML immediately after that tag.

    Login or Signup to reply.
  2. You may prefix your text to the standard Item.replyall.

    Option Explicit
    
    Sub LookForEmail(emailSubject As String)
       
        Dim olFolder As folder
        Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
        
        Dim mailFolderItems As Items
        Dim Item As Object
        
        Dim replyMail As MailItem
        
        Dim sFilter As String
        
        sFilter = "@SQL=""http://schemas.microsoft.com/mapi/proptag/0x0037001f"" like '%" & emailSubject & "%'"
        
        Set mailFolderItems = olFolder.Items.Restrict(sFilter)
        mailFolderItems.Sort "ReceivedTime", True
        
        For Each Item In mailFolderItems
        
            If TypeOf Item Is MailItem Then
                
                If Item.Subject = emailSubject Then
                    
                    Set replyMail = Item.replyall
    
                    With replyMail
                    
                        .Display    ' initial .htmlbody and default signature
                        
                        ' add to the initial .htmlbody
                        .htmlbody = "<HTML><Body><span>test</span><Body></HTML>" _
                          & "<span>" & "test2" & "</span>" _
                          & .htmlbody
                          
                    End With
                    
                    Exit For
                    
                End If
            End If
        Next
        
    End Sub
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search