有一些问题,试图复制透视表从2个单独的excel工作表,包含在1个工作簿到outlook电子邮件作为图片。(特别想这样做作为图片,而不是转换为HTML表)
我已经尝试了几种不同的方法,但不断得到错误-希望一些建议?
我试着设置它,使它每小时运行一次,每小时,只要工作簿是打开的。
目前为止我所拥有的代码是:
Sub RefreshAndEmailPivotTables()
Dim objOutlook As Object
Dim objEmail As Object
Dim wb As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim pt1 As PivotTable
Dim pt2 As PivotTable
Dim time As String
' Get the current time and format it as a string
time = Format(Now, "hh:mm")
' Set the worksheet and pivot table objects
Set wb = ThisWorkbook
Set ws1 = wb.Sheets("New Claims AHT")
Set ws2 = wb.Sheets("Existing Claims AHT")
Set pt1 = ws1.PivotTables("PivotTable1")
Set pt2 = ws2.PivotTables("PivotTable1")
' Refresh all data connections
ThisWorkbook.RefreshAll
' Create a new email in Outlook
Set objOutlook = CreateObject("Outlook.Application")
Set objEmail = objOutlook.CreateItem(0)
' Set the email properties and paste the pivot table images
With objEmail
.To = "@Leaders"
.CC = "@Colleagues"
.Subject = "Update - " & time
.Body = "Here are the pivot tables:" & vbNewLine & vbNewLine
.Body = .Body & "New Claims:" & vbNewLine
'COPY PIVOTTABLE1 FROM "New Claims AHT" Sheet and paste into email body - Advice here please? :)
.Body = .Body & vbNewLine & vbNewLine & "Existing Claims:" & vbNewLine
'COPY PIVOTTABLE1 FROM "Existing Claims AHT" Sheet - Advice here please? :)
.Display
End With
' Schedule the macro to run again in one hour
Application.OnTime TimeValue("01:00:00"), "RefreshAndEmailPivotTables"
' Clean up
Set objEmail = Nothing
Set objOutlook = Nothing
Set wb = Nothing
Set ws1 = Nothing
Set ws2 = Nothing
Set pt1 = Nothing
Set pt2 = Nothing
End Sub
我试过使用. copy和. paste,但是我得到一个对象不支持这个属性或方法错误。
1条答案
按热度按时间2w2cym1i1#
是的你可以做到这一点,你需要使用一个html主体,并使用我发现的这个函数来帮助导出范围为图像。
我只包含了生成电子邮件所需的部分。
记住
<br>
在HTML中是换行符