HotTomales
HotTomales

Reputation: 564

Add Text To Email Thread

I search my Sent Mail in Outlook and open the last email to a specified email address (this part is done).

I want to add text to the email chain, while keeping the previous messages intact.

The code below creates a "blank-slate" so that all of the previous email correspondence is lost.

What do I need to do to add text to the Body of the email?

FunctionComposeResponse(searchEmail As String, emailBody As String)
Dim currDateTime As Date: currDateTime = Now()
Dim tenDayPrior As Date: tenDayPrior = DateValue(CStr(Now())) - 10 & " 07:00:00 AM"
Dim olApp As Outlook.Application
Dim olNS As NameSpace
Dim Fldr As Folder
Dim olReply As Outlook.MailItem
Dim msg As Object
Set olApp = New Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set Fldr = olNS.GetDefaultFolder(olFolderSentMail)
For Each msg In Fldr.Items
  If TypeName(msg) = "MailItem" Then
    For Each recipient in msg.recipients
       If recip.Address = searchEmail Then
         If msg.SentOn >= tenDayPrior And msg.SentOn <= currDateTime Then
           Set olReply = msg.ReplyAll
           With olReply
              .BodyFormat = olFormatHTML
              .HTMLBody = emailBody
              .Save
              .Close olSave
           End With
         End If
       End If
    Next recip
  End If
Next msg
End Function

Upvotes: 1

Views: 577

Answers (1)

Andre
Andre

Reputation: 27644

By setting

.HTMLBody = emailBody

you overwrite everything that was there before.

You need to insert your text into the existing .HTMLBody.

For new mailitems, where I want to preserve the default HTML signature, I use the following - inspect your existing .HTMLBody to find out if this will also work for Reply (if not: adapt).

' emailBody is plain text -> encode as HTML
emailBody = HtmlEncode(emailBody)

' Outlook-HTML: mail text begins with this line:
'  <p class=MsoNormal><o:p>&nbsp;</o:p></p>
' Insert my text instead of the first &nbsp;
oItem.HtmlBody = Replace(oItem.HtmlBody, "&nbsp;", emailBody, Count:=1)

Upvotes: 1

Related Questions