Inserting a table into HtmlBody of email in vba

Tablecloth98

New Member
Joined
Nov 15, 2023
Messages
16
Office Version
  1. 2021
Platform
  1. Windows
I'm trying to insert a table whose content are contained on a spreadsheet (Let's say within the range A1:D4 as an example) into an email. I've written the body out in HTMLBody format and have followed the instructions from this guide to insert the table: Convert Excel Range Into HTML Table Through VBA 2024

When I run the Macro however, it produces an error and highlights some lines of the function in red. I've copied and pasted this directly from the guide so I'm not sure why it isn't working. Does anybody know why it's producing the error?
Error.jpg
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
It looks like copying and pasting the code from that page kinda messed it up a bit. Try copying and pasting the following code instead...

HTML:
'Following function converts Excel range to HTML table
Public Function ConvertRangeToHTMLTable(rInput As Range) As String
'Declare variables
 Dim rRow As Range
Dim rCell As Range
Dim strReturn As String
'Define table format and font
 strReturn = "<Table border='1' cellspacing='0' cellpadding='7' style='border-collapse:collapse;border:none'> "
'Loop through each row in the range
For Each rRow In rInput.Rows
 'Start new html row
strReturn = strReturn & " <tr align='Center'; style='height:10.00pt'> "
For Each rCell In rRow.Cells
'If it is row 1 then it is header row that need to be bold
 If rCell.Row = 1 Then
strReturn = strReturn & "<td valign='Center' style='border:solid windowtext 1.0pt; padding:0cm 5.4pt 0cm 5.4pt;height:1.05pt'><b>" & rCell.Text & "</b></td>"
 Else
 strReturn = strReturn & "<td valign='Center' style='border:solid windowtext 1.0pt; padding:0cm 5.4pt 0cm 5.4pt;height:1.05pt'>" & rCell.Text & "</td>"
 End If
 Next rCell
 'End a row
strReturn = strReturn & "</tr>"
 Next rRow
 'Close the font tag
strReturn = strReturn & "</font></table>"
'Return html format
ConvertRangeToHTMLTable = strReturn
End Function

Hope this helsp!
 
Upvote 1
Solution
That's solved it 🙂 thank you so much. You've saved me hours of troubleshooting
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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