使用Vba向2个或更多Excel工作簿发送电子邮件

时间:2012-02-29 16:43:46

标签: excel email vba excel-vba

我无法弄清楚如何为我的生活发送超过1个工作簿! 我知道通过电子邮件发送1个工作簿的几种不同方法,我将它们放在这里。

Sub SendActiveWorkbook()
                ActiveWorkbook.SendMail _
    Recipients:=Array("MyEmail@123.com", "AnotherEmail@123.com"), _
    Subject:="Write subject here"                 
End Sub

并且

Sub RouteActiveWorkbook()   
    With ActiveWorkbook
           .HasRoutingSlip = True
               With .RoutingSlip
                    .Delivery = xlAllAtOnce
                    .Recipients = Array("MyEmail@123.com", "AnotherEmail@123.com")
                    .Subject = "CSAM Lux BIEO and BCF breakdown"
                    .Message = "Attached are the breakdowns as of " & Date
               End With
            .Route
     End With
End Sub

我似乎只能在给定的电子邮件中发送1个工作簿。 (将我的2个工作簿变成1个工作簿并不能解决我的问题)。任何人都可以通过电子邮件发送超过1个工作簿吗?

1 个答案:

答案 0 :(得分:6)

希望这有帮助吗?

这是发送包含1个以上附件的电子邮件的基本示例。请根据实际情况进行修改。如果您有任何疑问,请告诉我。另外,我没有在下面的例子中处理错误处理。

已经过测试

Option Explicit

Sub Sample()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim MyFileList(1) As String
    Dim i As Long

    '~~> Change/Add the file names here
    MyFileList(0) = "C:\Sample1.xlsm"
    MyFileList(1) = "C:\Sample2.xlsm"

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    With OutMail
        .To = "MyEmail@123.com"
        .Subject = "Example for attaching 2 files"
        .Body = "Hi Ommit :)"

        For i = LBound(MyFileList) To UBound(MyFileList)
            .Attachments.Add MyFileList(i)
        Next i

        .Display
    End With
End Sub