user3728348
user3728348

Reputation: 81

Replace text in HTML body

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?

Upvotes: 5

Views: 12522

Answers (1)

user3728348
user3728348

Reputation: 81

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

Upvotes: 3

Related Questions