将特定文件附加到相应的收件人
Attaching Specific files to corresponding recipients
我有一长串成本报告要发送给不同的收件人。
我想我可以有一个 Excel 文件,其中包含地址和相应的位置,即
A1 John.smith@com.com A2 0001
B1 Jeff.smith@com.com B1 0002
然后使用 VBA 循环遍历每一行 (1) 并在文件夹中搜索相应的 (A2) 命名文件并将其附加到邮件中 (A1)。
我假设第一行有 headers。 未测试。
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
我有一长串成本报告要发送给不同的收件人。
我想我可以有一个 Excel 文件,其中包含地址和相应的位置,即 A1 John.smith@com.com A2 0001 B1 Jeff.smith@com.com B1 0002
然后使用 VBA 循环遍历每一行 (1) 并在文件夹中搜索相应的 (A2) 命名文件并将其附加到邮件中 (A1)。
我假设第一行有 headers。 未测试。
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