Using the RangeToHTML function from Ron de Bruin, I'm pasting a range into an outlook email. However, it seems that an extra blank row is being pasted into the email as seen below:
I've already confirmed that the Source:=TempWB.Sheets(1).UsedRange.Address
line is correctly grabbing only the data itself and not an extra line. I've also confirmed that the input range to RangetoHTML()
is also correct. My only guess is that the the .ReadAll
method is somehow putting an extra line in the file, but I'm not sure how to debug that. Here's the RangetoHTML
function I'm using for easy reference:
Function RangetoHTML(rng As Range)
' By Ron de Bruin.
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook
Application.ScreenUpdating = False
Application.EnableEvents = False
TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
'Copy the range and create a new workbook to past the data in
If rng Is Nothing Then GoTo Skip
rng.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
.Cells(1, 2).PasteSpecial Paste:=8
.Cells(1, 2).PasteSpecial xlPasteFormats
.Cells(1, 2).PasteSpecial xlPasteValues
.Cells.Font.Name = "Calibri"
.Cells(1).Select
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With
'Publish the sheet to a htm file
With TempWB.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=TempWB.Sheets(1).Name, _
Source:=TempWB.Sheets(1).UsedRange.Address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With
'Read all data from the htm file into RangetoHTML
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1)
RangetoHTML = ts.ReadAll
ts.Close
RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
"align=left x:publishsource=")
'Close TempWB
TempWB.Close savechanges:=False
'Delete the htm file we used in this function
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
Skip:
Application.ScreenUpdating = True
End Function
EDIT: Here's the portion of code where the email is being generated. The RangeToHTML(rng_Summary)
is what inserts the range into the email:
'Construct the actual email in outlook
With OutMail
.to = "LastName, FirstName"
.CC = ""
.BCC = ""
.Subject = "LOB Break Status (As of " & Format(Now(), "m/d") & ")"
.HTMLBody = "<BODY style=font-size:11pt;font-family:Calibri>Here is the latest status for the breaks, by product, in the LOB:" & _
RangetoHTML(rng_Summary) & _
"<BODY style=font-size:9pt;font-family:Calibri>*allows are excluded from Avg. Age of Breaks calculation" & _
"<ul>" & _
"<li>" & _
"<BODY style=font-size:11pt;font-family:Calibri><u><b>Average Age of Breaks</u></b>" & Chr(150) & " " & avg_age_change & " from " & avg_break_age_prev & " to " & avg_break_age_curr & " due to ________" & _
"</li>" & _
"</ul>"
.Display 'CHANGE THIS to .Display/.Send if you want to test/send
End With