I am currently working on a process automation for my work. Basically, the macro analyses the data and creates an email with results.
Everything works perfectly fine, besides the fact I have absolutely no clue how to implement my font formatting into Outlook's email body. I cannot set font formatting to constant value as each row's formatting varies and is depended on excel formulas and conditional formattings in my worksheet.
The function which creates the HTML body looks as below:
Private Function BodyBuilder(wsA As Worksheet) As String
Dim html As String
Dim sTTo As String
Dim rngSN As Range
Dim cl As Range
Dim sName As String
Set OutSh = ThisWorkbook.Sheets("Output")
sTTo = Comm.Cells(5, 4)
sName = OutSh.Cells(3, 2)
html = "Dear " & sTTo & ","
Set rngSN = OutSh.Range("a4:a11")
html = html & "<br><br><table><tr><td><strong>" & "Detailed risk report for selected company - " & sName & "</strong></td></tr>"
**For Each cl In rngSN
html = html & "<tr><td>" & Format(cl.Value, cl.NumberFormat) & "</td><td>" & cl.Offset(, 1).Value & "</td></tr>"
Next cl**
html = html & "</table>" & "<br>" & "Kind regards"
BodyBuilder = html
End Function
Basically, the email looks perfectly fine besides that I would like to see my values with it's font formats (bolded if needed and colors).
Can someone help with this?
Thank you
Wedge is correct that Ron de Bruin’s RangeToHtml is your first choice. However, this routine uses Excel Publish Objects which produce poor quality CCS with column widths set in pixels. At least some smartphones cannot process this CSS at all and others produce displays that are difficult to read. If none of your recipients will open your emails with a smartphone then Wedge’s solution will be adequate for your needs.
A second solution is a RangeToHtml routine that I wrote in VBA. This following extract from its introduction lists its handling of formatting:
The trouble with my routine is (1) it is not particularly fast and (2) its total size is way over Stack overflow’s limit of 30,000 characters per answer. If you need anything like this level of functionality, email me and I will send you my development workbook.
If all you need is bold and a few colours, you can code your own routine using my approach.
My html head element is something like this:
My routine generates just the styles it needs to handle the formats it finds in the Excel range. If you only need bold and a few colours, you could hardcode your own style element with shorter class names than I need.
The output for a row is then something like:
If either of the second and third approaches looks interesting but is unclear, post a comment and I will expand my explanations.
If your goal is to functionally be able to "copy-paste" some data from Excel into an Outlook e-mail while retaining all the formatting, it would probably be easiest to just use Ron de Bruin's function that converts an Excel range into HTML. I checked and this seems like it even retains formatting from conditional formatting.
Source website.
Then you can just get your data converted to HTML to put in the body tag like
(Repost of the function in question)