不同的数据透视表到不同的电子邮件vba

时间:2019-02-14 22:18:56

标签: vba outlook pivot-table

我有多个数据透视表,我想分别发送到不同的电子邮件中。问题是,该电子邮件不断被第二封电子邮件覆盖。每个数据透视表的名称都不同,并且该代码仅需一个引用即可正常工作。我尝试了多种方法来获得它,但是不能。有人可以帮忙吗?

Private Sub Workbook_Open()
Dim wk As Worksheet
Dim wk1 As Worksheet
wk = Worksheets("EPSICAR")
wk.Connections("owssvr").Refresh
wk1 = Worksheets("PastDue")
wk1.RefreshAll

End Sub

Sub pastdue()

Dim myApp As Outlook.Application, mymail As Outlook.Mailitem
Dim Lap As Object
Dim rng As Range
Dim rngmtl As Range
Dim Mailitem As Object

Set myApp = New Outlook.Application
Set mymail = myApp.CreateItem(olMailItem)
Set rng = Sheets("PastDue").PivotTables("Q Group Past 
Due").TableRange1
Set rngmtl = Sheets("PastDue").PivotTables("Mtl Group Past 
Due").TableRange2

With mymail
.To = "sponge.bobh@12345.com"
.CC = "pat.star@12345.com"
.Subject = "ICAR/EPS past due"
.HTMLBody = "The following are a list of ICARs/EPS that are past due" & 
 RangetoHTML(rng)
.Display
'.send


With mymail
.To = "blue.berry@12345.com"
.CC = "black.berry@12345.com"
.Subject = "ICAR/EPS past due"
.HTMLBody = "The following are a list of ICARs/EPS that are past due" & 
RangetoHTML(rngmtl)
.Display
'.send



Set myApp = Nothing
Set mymail = Nothing
End With
End With


End Sub

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

1 个答案:

答案 0 :(得分:0)

我认为您的方法有些偏离。我想有几种方法可以做这种事情。您可以通过多种方式刷新数据透视,然后将每种快照的电子邮件发送给不同的收件人。您可以采用多种方式刷新数据透视表,进行保存,然后将保存的版本通过电子邮件发送给不同的人员。如果您想走这条路,请尝试在下面的代码示例。

使用:在Sheets(“ Sheet1”)中创建列表:

In column A : Names of the people
In column B : E-mail addresses
In column C:Z : Filenames like this C:\Data\Book2.xls (don't have to be Excel files)

宏将循环遍历“ Sheet1”中的每一行,并且如果B列中有电子邮件地址 和C:Z列中的文件名,它将创建包含此信息的邮件并发送。

Sub Send_Files()
'Working in Excel 2000-2016
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
    Dim OutApp As Object
    Dim OutMail As Object
    Dim sh As Worksheet
    Dim cell As Range
    Dim FileCell As Range
    Dim rng As Range

    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

    Set sh = Sheets("Sheet1")

    Set OutApp = CreateObject("Outlook.Application")

    For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)

        'Enter the path/file names in the C:Z column in each row
        Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1")

        If cell.Value Like "?*@?*.?*" And _
           Application.WorksheetFunction.CountA(rng) > 0 Then
            Set OutMail = OutApp.CreateItem(0)

            With OutMail
                .to = cell.Value
                .Subject = "Testfile"
                .Body = "Hi " & cell.Offset(0, -1).Value

                For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
                    If Trim(FileCell) <> "" Then
                        If Dir(FileCell.Value) <> "" Then
                            .Attachments.Add FileCell.Value
                        End If
                    End If
                Next FileCell

                .Send  'Or use .Display
            End With

            Set OutMail = Nothing
        End If
    Next cell

    Set OutApp = Nothing
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
End Sub

您可能还会考虑其他两种方法。在尝试使用不同的方法达到同一目的之前,只需做一件事即可。