Greg Lovern
Greg Lovern

Reputation: 977

How to replace text in Outlook email body without deleting images

In Excel 2016 VBA, I'm automating Outlook to replace text in the body of emails. The part that does the replacement looks like this:

Dim oEmail As Object, strEmailSubject As String, strEmailBody As String
Set oEmail = GetActiveOutlookEmail
strEmailBody = TranslateOneEmailElement(oEmail.Body)
If strEmailBody <> oEmail.Body Then oEmail.Body = strEmailBody

(I've left out the GetActiveOutlookEmail and TranslateOneEmailElement functions as I don't think they are relevant to the question and would just add a lot of clutter.)

That works fine for the text. However, if the email body had any images, that line deletes them.

How can I replace text line by line so that it leaves images in place?

Upvotes: 0

Views: 1043

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66215

You are reading and setting the plain text Body property wiping out all the formatting, not just images. You need to work with the HTMLBody property instead.

Upvotes: 1

Related Questions