Hi! I'm running the popular Excel-to-Outlook VBA code by Ron de Bruin. Granted, this seems like a miniature issue (and it is), but it still drives me crazy. As the code stands, I'm missing the bottom end of the table frame in Outlook (screenshots from Excel table + 2 below from Outlook messages). Running the Office365 package.
I only experience correct table framing in Outlook in case I start with .Display command in the With OutMail segment (currently commented out). However this causes the Outlook message opening flicker, which is best avoided - if possible. Might someone also had a similar problem and knows a fix this without the .Display command?
Unfortunately not 100% consistent, but the missing bottom frame seemed to occur when the pasted table had been filtered. Have tried different scenarios with temporary workbook Paste options as well as tinkered with mail body settings, but haven't figured this one out.
I only experience correct table framing in Outlook in case I start with .Display command in the With OutMail segment (currently commented out). However this causes the Outlook message opening flicker, which is best avoided - if possible. Might someone also had a similar problem and knows a fix this without the .Display command?
Unfortunately not 100% consistent, but the missing bottom frame seemed to occur when the pasted table had been filtered. Have tried different scenarios with temporary workbook Paste options as well as tinkered with mail body settings, but haven't figured this one out.
VBA Code:
Sub Table_to_Email()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("X")
Dim rng As Range
Set rng = ws.ListObjects("myTable").Range.SpecialCells(xlCellTypeVisible)
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
'.Display
.To = "myemail@gmail.com"
.CC = ""
.BCC = ""
.Subject = "Testmail"
.HtmlBody = RangetoHTML(rng)
.Send
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
ThisWorkbook.Saved = True
End Sub
Function RangetoHTML(rng As Range)
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook
TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
rng.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
.Cells(1).Select
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With
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
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.readall
ts.Close
RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
"align=left x:publishsource=")
TempWB.Close savechanges:=False
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function