How to change the font color in email body with VBA

Ramadan

Board Regular
Joined
Jan 20, 2024
Messages
193
Office Version
  1. 2021
Platform
  1. Windows
I have a code to send email with attachement from excel and I need to change font color of some text in the email body but I don't know how to do that
in the below part of the code, I need the (period) text to be in red and also (body6) to be in red with underline below the word
(period) and (body6) refer to cell value in the sheet

thank you in advance

VBA Code:
newHTML = "<div dir='rtl' style='font-family:Calibri; font-size:11pt;'<b>" & _
              "<p>" & body1 & "<p>" & _
               "<p>" & body7 & "</p>" & _
              "<p>" & body2 & " <b>(" & membershipNo & ")</b> " & body3 & " <b>(" & _
              startDate & "</b>  -  <b> " & endDate & ")</b> " & period & " </p>" & _
              "<p>" & body4 & "</p>" & _
              "<p>" & body5 & "</p>" & _
              "<b>" & body6 & "</p>" & _
              "</div>"""
 
Within your HTMLBody you would add a style and font attributes. Take a look here at W3School which will help with lots of HTML topics.

W3Schools.com
 
Upvote 0
Please clarify the code for & body 6 as you should be able to add the style, colour and underlining regardless of a cell value or & body 6....

What is & body 6? As your code is an extract of your overall code.
 
Upvote 0
Please clarify the code for & body 6 as you should be able to add the style, colour and underlining regardless of a cell value or & body 6....

What is & body 6? As your code is an extract of your overall code.
@Trevor G here is my entire code

VBA Code:
Public Sub Send_Email()

    On Error GoTo CleanFail

    Dim ws As Worksheet
    Set ws = ActiveSheet

    If ActiveCell.Column <> 7 Then
        MsgBox "Please click a cell in column G to send the email.", vbExclamation
        Exit Sub
    End If

    Dim activeRow As Long
    activeRow = ActiveCell.Row

    Dim membershipNo As String, startDate As String, endDate As String, period As String
    
    membershipNo = Trim(ws.Cells(activeRow, "B").Value)
    startDate = Trim(ws.Cells(activeRow, "J").Value)
    endDate = Trim(ws.Cells(activeRow, "K").Value)
    period = Trim(ws.Cells(activeRow, "M").Value)
    
    Dim ToEmail As String, CCEmail As String
    ToEmail = Trim(ws.Range("R9").Value)
    CCEmail = Trim(ws.Range("R10").Value)
    Subject = Trim(ws.Range("R11").Value)
    

    If membershipNo = "" Or startDate = "" Or endDate = "" Then
        MsgBox "Membership No, Start Date, or End Date is missing in row " & _
               activeRow, vbCritical
        Exit Sub
    End If

    If ToEmail = "" Then
        MsgBox "The 'To' email address in cell R9 is missing.", vbCritical
        Exit Sub
    End If

    Dim outApp As Object
    On Error Resume Next
    Set outApp = CreateObject("Outlook.Application")
    On Error GoTo CleanFail

    If outApp Is Nothing Then
        MsgBox "Outlook is not available on this system.", vbCritical
        Exit Sub
    End If

    Dim outMail As Object
    Set outMail = outApp.CreateItem(0)

    Dim body1 As String, body2 As String, body3 As String
    body1 = ws.Range("R12").Value
    body2 = ws.Range("R13").Value
    body3 = ws.Range("R14").Value
    body4 = ws.Range("R15").Value
    body5 = ws.Range("R16").Value
    body6 = ws.Range("R17").Value
    body7 = ws.Range("R18").Value
    
    Dim newHTML As String
    newHTML = "<div dir='rtl' style='font-family:Calibri; font-size:11pt;'>" & _
              "<p>" & body1 & "</p>" & _
               "<p>" & body7 & "</p>" & _
              "<p>" & body2 & " <b>(" & membershipNo & ")</b> " & body3 & " <b>(" & _
              startDate & "</b>  -  <b> " & endDate & ")</b> " & period & " </p>" & _
              "<p>" & body4 & "</p>" & _
              "<p>" & body5 & "</p>" & _
              "<p>" & body6 & "</p>" & _
              "</div>"""

    Dim HTML As String
    With outMail
        .GetInspector
        HTML = .HTMLBody
    End With

    Dim p1 As Long, p2 As Long
    p1 = InStr(1, HTML, "<p ", vbTextCompare)
    p2 = InStr(p1 + 1, HTML, "<p ", vbTextCompare)
    p2 = InStr(p2, HTML, "</p>")
    HTML = Left(HTML, p1 - 1) & _
           Mid(HTML, p2 + Len("</p>"))

    p1 = InStr(1, HTML, "<body", vbTextCompare)
    p1 = InStr(p1, HTML, ">")
    HTML = Left(HTML, p1) & _
           newHTML & _
           Mid(HTML, p1 + 1)

    Dim filePath As String
    filePath = "D:\Desktop\Guards\gurads list.xlsm" 'Change if different

    If Dir(filePath) = "" Then
        MsgBox "Attachment not found: " & filePath, vbCritical
        Exit Sub
    End If

    With outMail
        .To = ToEmail
        .CC = CCEmail
        .Subject = Subject & "  " & "(" & membershipNo & ")"
        .HTMLBody = HTML
        .Attachments.Add filePath
        .Display ' Change to .Send to send automatically
    End With

    Set outMail = Nothing
    Set outApp = Nothing
    Exit Sub

CleanFail:
    MsgBox "Error occurred: " & Err.Description, vbCritical

End Sub
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top