我曾经使用过 Ron de Bruin 的 RangeToHTML,效果很好。
将此代码放在标准模块上并尝试一下。我使用了两个范围,一个用于枢轴过滤器,另一个用于枢轴体
Sub EmailSend()
Dim olApp As Outlook.Application
Set olApp = New Outlook.Application
Dim olEmail As Outlook.MailItem
Set olEmail = olApp.CreateItem(olMailItem)
With olEmail
' Recipients
.To = "c_dpal###in@###.com.##"
.CC = "c_dpal###in@###.com.##"
' Subject
.Subject = ActiveWorkbook.Sheets("Sheet1").Range("J3")
' Mail body
.BodyFormat = olFormatHTML
.display ' display comes here so that the signature can be shown if it exists
Dim strBody As String
Dim PivotRng As Range
Dim PivotFilterRng As Range: Set PivotFilterRng = ActiveSheet.Range("B4:C6")
' Greetings
strBody = strBody & "Good Day Ms ###," & "<br><br>"
strBody = strBody & "Please see performance as of today:" & "<br><br>"
' Pivot Filters
strBody = strBody & RangetoHTML(PivotFilterRng)
' Pivot body
Set PivotRng = ActiveSheet.PivotTables(1).TableRange1
strBody = strBody & RangetoHTML(PivotRng) & "<br>"
strBody = strBody & "Regards," & "<br>" & "####"
' Insert 'strBody'
.HTMLBody = strBody & .HTMLBody
' Send mail
'.send
End With
End Sub
Ron de Bruin 函数
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