Replace text in HTML body
10,710
I'm not sure how to tag or flag an answer for this but here is the code that I got to work from the editing provided by Tim Williams, thank you for your help with this:
Sub NewUserEmail()
Dim myItem As Outlook.MailItem
Dim strContact As String
Dim strCompanyName As String
Dim strHTML As String
Set myItem = Application.CreateItemFromTemplate("C:\file location\file.oft")
strHTML = myItem.HTMLBody
strContact = InputBox("What is the Contact's name?")
myItem.HTMLBody = Replace(myItem.HTMLBody, "%CONTACT%", strContact)
myItem.Display
End Sub
Author by
user3728348
Updated on June 27, 2022Comments
-
user3728348 almost 2 years
I'm trying to replace text in the body of a template already created in Outlook 2010. The purpose of this is so that users can update the contact which the email is being sent to fairly easily.
Sub NewUserEmail() Dim myItem As Outlook.MailItem Dim strContact As String Dim strCompanyName As String Dim strHTML As String Set myItem = Application.CreateItemFromTemplate( _ "C:\Users\jim.reagan\AppData\Roaming\Microsoft\Templates\NewUserEmail.oft") strHTML = myItem.HTMLBody strContact = InputBox("What is the Contact's name?") myItem.HTMLBody = Replace(myItem.HTMLBody, "%<Contact>%", strContact) myItem.Display End Sub
The template opens up for review but no replacements have been made to the body of the email. If I use myItem.Body the replacement works but then I lose my formatting of my email. What am I missing?
-
Dmitry Streblechenko almost 10 yearsAre you sure the HTMLBody actually contains "%%"?
-
user3728348 almost 10 yearsIts part of the string that I put in body of the email. Something used from boilerplate template.
-
Dmitry Streblechenko almost 10 yearsOk, but can you output (MsgBox?) the contents of the HTMLBody property before and after setting the property?
-
SOSidb almost 7 years@Dmitry Streblechenko - thank you very much. I ran into this same issue and your idea helped point me in the right direction (by using a write-host versus MsgBox). But I found that the process was inserting additional code into the template HTMLbody that was separating the phrase that I was trying to "replacing". I tried using % and ! and others, but HTML kept separating them from the phrase itself. The actual HTML code being produced was "</span>!<span class=SpellE>" (w/o quotes) when I used the exclamation point. So I removed all "special" characters and used letters only. Thanks again
-