Search code examples
vbareplaceoutlookmailitem

Outlook MailItem HTMLBody Replace() not working - brand new be gentle


Okay here is the updated version... still not working though

        NextHTMLBody = nextMessage.HTMLBody
        NextHTMLBody = Replace(NextHTMLBody, "%Customer_Name%", bBodyName)
        NextHTMLBody = Replace(NextHTMLBody, "%##%", formPage.Controls("LoanNumber1").Value)

        With nextMessage
            .HTMLBody = NextHTMLBody
            .Subject = subjEmail
            .To = sendTo
            .Send
        End With

-------------and here it works.. not sure what I did differently

        'Set body
        NextHTMLBody = nextMessage.HTMLBody
        NextHTMLBody = Replace(NextHTMLBody, "%Customer_Name%", bBodyName)
        NextHTMLBody = Replace(NextHTMLBody, "%##%", formPage.Controls("LoanNumber1").Value)

        With nextMessage
            .HTMLBody = NextHTMLBody
            '.Body = NextHTMLBody
            .Subject = subjEmail
            .To = sendTo
            '.Send
            .Display
        End With

Solution

  • Replace should work OK here:

    If InStr(nextMessage.HTMLBody, "%Customer_Name%") <> 0 Then
        NextHTMLBody = Replace(nextMessage.Body, "%Customer_Name%", bBodyName)
    End If
    

    But in the next block you're discarding the replacement you performed above by going back to nextMessage.HTMLBody instead of continuing with NextHTMLBody. You're also switching to HTMLBody from Body: not sure if there's a reson for that.

    If InStr(nextMessage.HTMLBody, "%##%") <> 0 Then
    
        NextHTMLBody = Replace(nextMessage.HTMLBody, "%##%", _
                                 formPage.Controls("LoanNumber1").Value)
    End If
    

    Best bet is something more like this (you don't really need the If-Then here):

    NextHTMLBody = nextMessage.HTMLBody
    
    NextHTMLBody = Replace(NextHTMLBody, "%Customer_Name%", bBodyName)
    
    NextHTMLBody = Replace(NextHTMLBody, "%##%", _
                                 formPage.Controls("LoanNumber1").Value)