Function to convert to HTML for email with specific font

Bering

Board Regular
Joined
Aug 22, 2018
Messages
185
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I use the below brilliant function (which I found somewhere on the internet) to convert to HTML the content of a cell (H9) in order to use it in the body of automatic emails and it works great. This is very helpful, because the body of my email is quite complex, with a lot of concatenations of strings and variables.

The only thing I am not happy with is that email gets always created in a Calibri font, even though the default one in Outlook is "Raleway".

I do not know how I could change the below code to achieve this...

Any ideas?

Thanks.

VBA Code:
ActiveSheet.Range("H9") = "=fnConvert2HTML(RC[-1])"
       
        strBody = ActiveSheet.Range("H9")
       
       
        'COMBINE THE EMAIL WITH THE SIGNATURE
        .HTMLBody = strBody & strSig



VBA Code:
Function fnConvert2HTML(myCell As Range) As String
    Dim bldTagOn, itlTagOn, ulnTagOn, colTagOn As Boolean
    Dim i, chrCount As Integer
    Dim chrCol, chrLastCol, htmlTxt As String
   
    bldTagOn = False
    itlTagOn = False
    ulnTagOn = False
    colTagOn = False
    chrCol = "NONE"
    htmlTxt = "<html>"
 
    chrCount = myCell.Characters.Count
   
    For i = 1 To chrCount
        With myCell.Characters(i, 1)
       
            If (.Font.Color) Then
                If Not colTagOn Then
                    htmlTxt = htmlTxt & "<font color=#" & chrCol & ">"
                    colTagOn = True
                Else
                    If chrCol <> chrLastCol Then htmlTxt = htmlTxt & "</font><font color=#" & chrCol & ">"
                End If
            Else
                chrCol = "NONE"
                If colTagOn Then
                    htmlTxt = htmlTxt & "</font>"
                    colTagOn = False
                End If
            End If
            chrLastCol = chrCol
           
            If .Font.Bold = True Then
                If Not bldTagOn Then
                    htmlTxt = htmlTxt & "<b>"
                    bldTagOn = True
                End If
            Else
                If bldTagOn Then
                    htmlTxt = htmlTxt & "</b>"
                    bldTagOn = False
                End If
            End If
   
            If .Font.Italic = True Then
                If Not itlTagOn Then
                    htmlTxt = htmlTxt & "<i>"
                    itlTagOn = True
                End If
            Else
                If itlTagOn Then
                    htmlTxt = htmlTxt & "</i>"
                    itlTagOn = False
                End If
            End If
   
            If .Font.Underline > 0 Then
                If Not ulnTagOn Then
                    htmlTxt = htmlTxt & "<u>"
                    ulnTagOn = True
                End If
            Else
                If ulnTagOn Then
                    htmlTxt = htmlTxt & "</u>"
                    ulnTagOn = False
                End If
            End If
           
            If (Asc(.Text) = 10) Then
                htmlTxt = htmlTxt & "<br>"
            Else
                htmlTxt = htmlTxt & .Text
            End If
        End With
    Next
   
    If colTagOn Then
        htmlTxt = htmlTxt & "</font>"
        colTagOn = False
    End If
    If bldTagOn Then
        htmlTxt = htmlTxt & "</b>"
        bldTagOn = False
    End If
    If itlTagOn Then
        htmlTxt = htmlTxt & "</i>"
        itlTagOn = False
    End If
    If ulnTagOn Then
        htmlTxt = htmlTxt & "</u>"
        ulnTagOn = False
    End If
    htmlTxt = htmlTxt & "</html>"
   
    fnConvert2HTML = htmlTxt
   
   
End Function
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
After several attempts I managed to get the string "strBody" in Raleway as body of the email , which of course is not what I want... lol

VBA Code:
 ActiveSheet.Range("H9") = "=fnConvert2HTML(RC[-1])"
        
        strBody = ActiveSheet.Range("H9")
        strBody = "<BODY style=font-size:11pt;font-family:Raleway> strBody  </BODY>"


I tried to change the above as follows but it gives me a syntax error message:

VBA Code:
strBody = ActiveSheet.Range("H9")
        strBody = "<BODY style=font-size:11pt;font-family:Raleway> ActiveSheet.Range("H9") </BODY>"

I am stuck.... any ideas please? :cry: :unsure:
 
Upvote 0
Problem solved :biggrin:, thanks.

VBA Code:
ActiveSheet.Range("H9") = "=fnConvert2HTML(RC[-1])"
        
        strBody = ActiveSheet.Range("H9")
        strBody = "<font style=""font-family: Raleway; font-size: 11pt;""/font>" & strBody
 
Upvote 0
Solution

Forum statistics

Threads
1,215,243
Messages
6,123,837
Members
449,129
Latest member
krishnamadison

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