将特定文件附加到相应的收件人

时间:2015-10-07 15:29:51

标签: vba excel-vba outlook-vba mailing-list excel

我有很长的费用报告列表要发送给不同的收件人。

我原以为我可以有一个带有地址和相应位置的Excel文件,即 A1 John.smith@com.com A2 0001 B1 Jeff.smith@com.com B1 0002

然后在每行(1)中使用VBA循环并在文件夹中搜索相应的(A2)命名文件,并将其附加到邮件到单元格(A1)。

1 个答案:

答案 0 :(得分:1)

我假设你在第一行有标题。的未测试

Sub AntMan()

Dim OutLookApp As Object
Dim OutLookMailItem As Object
Dim lastRow As Long
Dim MailDest As String
Dim subj As String

lastRow = ThisWorkbook.WorkSheets("Sheet6").Cells(Rows.Count, "A").End(xlUp).Row 'change worksheet

For i = 2 To lastRow

    Set OutLookApp = CreateObject("Outlook.application")
    Set OutLookMailItem = OutLookApp.CreateItem(0)
    Set Attach = OutLookMailItem.Attachments

    With OutLookMailItem
        .To = Cells(i, 1).Value
        .SUBJECT = "Put your subject here"
        .Body = "Put your body here"
        Attach.Add "C:\your\file\path\here\" & Cells(i, 2).Value & ".xlsx"
        .Display 'for debugging
        .Send
    End With

Next

End Sub