将特定文件附加到相应的收件人
我有一长串成本报告要发送给不同的收件人。将特定文件附加到相应的收件人
我想我可以有一个用的Excel文件的地址和相应的位置即 A1 [email protected] A2 0001 B1 [email protected] B1技术
然后使用VBA循环每行(1)并搜索相应的(A2)命名文件的文件夹并将其附加到邮件输出到单元格(A1)。
我假设你在第一行有标题。 未经测试。
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
你没有前景?其他电子邮件客户端可以像“Windows Live 2012”或“Thunderbird”一样使用。 – HarveyFrench
@HarveyFrench OP没有指定。 – findwindow
谢谢像魅力一样工作。我正在使用Outlook。 – AntMan
你是在正确的轨道上。 A栏有电子邮件。 B列有文件位置。循环访问列A以获取电子邮件和列B附加。这个链接是一个很好的开始。 http://*.com/questions/17883088/excel-vba-sending-mail-using-outlook-send-method-fails。 (不知道为什么用户对'.Send'有问题,但它总是适用于我。)因此,在工作簿中设置一个循环,并应用列A中的“To”和列B中的“Attachment”,并填充其余部分如所须。 –