Excel范围到Outlook电子邮件正文与自动调整

时间:2017-07-06 03:14:51

标签: excel vba excel-vba outlook

我想问一下是否可以自动复制复制到电子邮件正文的excel范围?当它自动粘贴/使用代码时,excel范围看起来很乱。但是当我手动复制并粘贴它时,它看起来更好。

自动粘贴/使用VBA的示例照片:USING VBA

手册示例照片(对我来说好多了):MANUALLY COPIED

我工作的最新更新:LATEST

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).Select
    .Cells(1).EntireRow.AutoFit
    .Cells(1).EntireColumn.AutoFit
    Application.CutCopyMode = False
    On Error Resume Next
    .DrawingObjects.Visible = True
    .DrawingObjects.Delete
    On Error GoTo 0
End With
TempWB.Sheets(1).UsedRange.Columns.AutoFit


'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

对此有何建议?提前致谢。顺便说一下,这些代码可以归功于Ron De Bruin。

0 个答案:

没有答案