粘贴到outlook时保持行格式

时间:2018-03-05 13:16:56

标签: excel vba outlook

以下是附加到sub的函数,该函数将Excel中的表格范围打印到Outlook电子邮件中。

虽然粘贴了列格式,但行高不是。有没有办法保持格式?我已尝试过以下"'.Cells(1).PasteSpecial xlPasteAllUsingSourceTheme, , False, False",但它似乎也无效。

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"

'Copy the range and create a new workbook to past the data in
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).PasteSpecial xlPasteAllUsingSourceTheme, , False, False
    .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, -2)
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

End Function

This is how tables from different tabs look after pasting into Email. Row height is fixed regardless. Only column width is variable. 这就是来自不同标签的表格粘贴到电子邮件后的样子。行高无论如何都是固定的。只有列宽是可变的。

0 个答案:

没有答案