Search code examples
excelvbaoutlookhtml-email

How to change Text colour in an HTML email generated by Excel VBA


I am trying to write VBA code that generates the body of an HTML email. Everything works as expected except when I try and change the color of the text for the signature, which needs to be in corporate colours, compared to the body which needs to be black.

I've stripped back the code to the minimum needed to demonstrate the issue. When i run this, it generates the email fine, and the text is correct, but the colour is wrong.

Sub Send_Email_Main_TEST()

    Dim OutApp As Object
    Dim OutMail

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    
    On Error Resume Next
    With OutMail
        .Subject = "TEST"
        .HTMLBody = "<sig style='font-size:10.0pt;color:blue;'><b>NAME | TITLE | DEPT | COMPANY</b><br>Tel Int: | Tel Ext: | Email: <sig>"
        .Display
    End With
    On Error GoTo 0
    
    Set OutMail = Nothing
    Set OutApp = Nothing
    
End Sub

This gives an email that looks like the below, with black text

Email Screenshot showing black text

However, when i run the HTML

<sig style='font-size:10.0pt;color:blue;'><b>NAME | TITLE | DEPT | COMPANY</b><br>Tel Int: | Tel Ext: | Email: <sig>

on www.W3Schools.com, the text is displayed as blue, as below: enter image description here

so as far as I can tell theres nothing wrong with the HTML, and as I've already said the VBA works in all other respects.


Solution

  • For whatever reason, the styles are not being applied to the sig tag. Wrapping the text content of the sig tag in another element and applying the styles to the inner element will fix the formatting.

    .HTMLBody = "<sig><b><span style='font-size:10.0pt;color:blue;'>NAME | TITLE | DEPT | COMPANY</span></b><br>Tel Int: | Tel Ext: | Email: <sig>"
    

    Result