'How to Left-Align HTML Table, created from an Excel Range, in an Outlook Body?
I want to copy a selected range from Excel to the body of an Outlook mail (Office 2013).
The table is pasting with Center Align.
I want to paste with left-align or modify the HTML table object to change its properties (e.g. alignment, word wrapping as is done with WordEditor Inspector).
I tried objNewEmail.HTMLBody = "<table align = ""left"">" & objTextStream.ReadAll & "</table>"
.
This embeds my table within another table and inexplicably, the new table is generated with Text-Wrapping on.
I tried <p align>
and a couple CSS styling options none of which gave any errors but they didn't change anything on the email.
Sub Macro1()
Selection.CurrentRegion.Select
Dim objSelection As Excel.Range
Dim objTempWorkbook As Excel.Workbook
Dim objTempWorksheet As Excel.Worksheet
Dim strTempHTMLFile As String
Dim objTempHTMLFile As Object
Dim objFileSystem As Object
Dim objTextStream As Object
Dim objOutlookApp As Outlook.Application
Dim objNewEmail As Outlook.MailItem
'Copy the selection
Set objSelection = Selection
Selection.Copy
'Paste the copied selected ranges into a temp worksheet
Set objTempWorkbook = Excel.Application.Workbooks.Add(1)
Set objTempWorksheet = objTempWorkbook.Sheets(1)
'Keep the values, column widths and formats in pasting
With objTempWorksheet.Cells(1)
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteColumnWidths
.PasteSpecial xlPasteFormats
End With
'Save the temp worksheet as a HTML file
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
strTempHTMLFile = objFileSystem.GetSpecialFolder(2).Path & "\Temp for Excel" & Format(Now, "YYYY-MM-DD hh-mm-ss") & ".htm"
Set objTempHTMLFile = objTempWorkbook.PublishObjects.Add(xlSourceRange, strTempHTMLFile, objTempWorksheet.Name, objTempWorksheet.UsedRange.Address)
objTempHTMLFile.Publish (True)
'Create a new email
Set objOutlookApp = CreateObject("Outlook.Application")
Set objNewEmail = objOutlookApp.CreateItem(olMailItem)
'Read the HTML file data and insert into the email body
Set objTextStream = objFileSystem.OpenTextFile(strTempHTMLFile)
objNewEmail.HTMLBody = "<p> See below table. </p>" & objTextStream.ReadAll
objNewEmail.HTMLBody = "<table align=""left"">" & objTextStream.ReadAll & "</table>" & "<p align=""left"">" & objNewEmail.HTMLBody & "</p>"
objNewEmail.HTMLBody = objNewEmail.HTMLBody & "<p> Thank you </p>"
objNewEmail.Display
objTextStream.Close
objTempWorkbook.Close (False)
objFileSystem.DeleteFile (strTempHTMLFile)
End Sub
Solution 1:[1]
A bit late but maybe the solution I found could help. By reading RangetoHTML
value, I noticed that the replacement of align=center x:publishsource=
with align=left x:publishsource=
is not occurring because there is a line break after align=center
.
To fix the problem, just use
RangetoHTML = Replace(RangetoHTML, "align=center", "align=left")
instead of
RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", "align=left x:publishsource=")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ussy1649 |