使用VBA向多个收件人发送邮件并复制并粘贴到正文
问题描述:
我想要一个excel文件并创建电子邮件。该文件可能具有多个具有相同电子邮件地址的行。我想为每个唯一地址创建一封电子邮件,并为具有相同地址的行创建一个表格以复制并粘贴到电子邮件中。使用VBA向多个收件人发送邮件并复制并粘贴到正文
我是VBA的新手,但创建了循环Excel文件以创建电子邮件的代码,但是,我需要修改代码以仅查看唯一地址和创建表的帮助。
我现在的代码如下:
Sub SendEmail()
'Uses late binding
Dim OutlookApp As Object
Dim MItem As Object
Dim cell As Range
Dim Subj As String
Dim Rname As String
Dim EmailAddr As String
Dim Rdate As String
Dim Ramount As String
Dim Vendor As String
Dim CHCPName As String
Dim HCPLast As String
Dim Repname As String
Dim Msg As String
'Dim FName As String
'Dim FLoc As String
'Create Outlook object
Set OutlookApp = CreateObject("Outlook.Application")
'Loop through the rows
For Each cell In Columns("A").Cells.SpecialCells(xlCellTypeConstants)
If cell.Value Like "*@*" Then
'Get the data
EmailAddr = cell.Value
Subj = "Meals with HCPs"
Repname = cell.Offset(, 1)
Rname = cell.Offset(, 2)
Rdate = cell.Offset(, 3)
Ramount = cell.Offset(, 4).Text
Vendor = cell.Offset(, 5)
CHCPName = cell.Offset(, 6)
'FName = cell.Offset(, 9)
'FLoc = cell.Offset(, 10)
'Compose message
Msg = "Dear " & Repname & ","
Msg = Msg & "<br/>"
Msg = Msg & "<br/>"
Msg = Msg & "In a recent review of expense report transactions for Federal Open Payments/Sunshine report, we"
Msg = Msg & " noticed that an incorrect expense type was selected for one or more of your meetings. On the following "
Msg = Msg & "report, you selected an incorrect expense type of " & "<b>Meals w/non HCPs out of office.</b> It appears that there were HCPs present during the meeting(s)."
Msg = Msg & "<br/>"
Msg = Msg & "<br/>"
Msg = Msg & "Please make sure that going forward, you select a correct expense type for all meetings with HCPs " & "<b>(Example: Meal w/HCP out Office-Non-Promo).</b>"
Msg = Msg & " We need to ensure that we are reporting correct information. Please note that future violations could result "
Msg = Msg & " in notification to your manager. If you have any questions, please let me know."
Msg = Msg & "<br/>"
Msg = Msg & "<br/>"
Msg = Msg & "<b>Expense Report Details:</b>"
Msg = Msg & "<br/>"
Msg = Msg & "<br/>"
Msg = Msg & "<b>Report Name: </b>" & Rname
Msg = Msg & "<br/>"
Msg = Msg & "<br/>"
Msg = Msg & "<b>Date: </b>" & Rdate
Msg = Msg & "<br/>"
Msg = Msg & "<br/>"
Msg = Msg & "<b>Amount: </b>" & Ramount
Msg = Msg & "<br/>"
Msg = Msg & "<br/>"
Msg = Msg & "<b>Vendor Name: </b>" & Vendor
Msg = Msg & "<br/>"
Msg = Msg & "<br/>"
Msg = Msg & "<b>HCP Name(s): </b>" & CHCPName
Msg = Msg & "<br/>"
Msg = Msg & "<br/>"
Msg = Msg & "Regards"
Msg = Msg & "<br/>"
Msg = Msg & "<br/>"
Msg = Msg & "Sunil Kumar"
Msg = Msg & "<br/>"
Msg = Msg & "Manager"
Msg = Msg & "<br/>"
Msg = Msg & "[email protected]"
Msg = Msg & "<br/>"
Msg = Msg & "+1(817)615-2333"
'Create Mail Item and send it
Set MItem = OutlookApp.CreateItem(0) 'olMailItem
With MItem
.to = EmailAddr
.Subject = Subj
.HTMLBody = Msg
'Add Atttachments here if you would like
'.Attachments.Add FLoc & FName
.Save 'to Drafts folder
'.Send does not work due to Macro Security Settings for Alcon. Must send using Outlook
End With
End If
Next
Set OutlookApp = Nothing
End Sub
答
正如@ K.Davis指出,你可以使用一个字典或集合,以测试重复。这里我使用一个ArrayList。
理想情况下,一个子程序应该执行1个任务。你应该把大的子程序分解成执行特定任务的小程序。这将使调试你的代码变得更容易。
Sub SendEmail()
'Uses late binding
Dim list As Object, OutlookApp As Object
Dim cell As Range
Dim HTMLBody As String
'Create Outlook object
Set OutlookApp = CreateObject("Outlook.Application")
'Loop through the rows
For Each cell In Columns("D").Cells.SpecialCells(xlCellTypeConstants)
If cell.value Like "*@*" And Not list.Contains(cell.value) Then
list.Add cell.value
HTMLBody = getMessageBody(cell.Offset(, 1), cell.Offset(, 2), cell.Offset(, 3), cell.Offset(, 4).Text, cell.Offset(, 5), cell.Offset(, 6))
EmailAddr = cell.value
CreateEmail OutlookApp, cell.value, "Meals with HCPs", HTMLBody
End If
Next
Set OutlookApp = Nothing
End Sub
Sub CreateEmail(OutlookApp As Object, EmailAddr As String, Subject As String, HTMLBody As String)
Dim MItem As Object
Set MItem = OutlookApp.CreateItem(0) 'olMailItem
With MItem
.to = EmailAddr
.Subject = Subj
.HTMLBody = Msg
.Save 'to Drafts folder
End With
End Sub
Function getMessageBody(Repname As String, Rname As String, Rdate As String, Ramount As String, Vendor As String, CHCPName As String)
Dim Msg As String
Msg = "Dear " & _
Repname
Msg = Msg & "<br/><br/>" & _
"In a recent review of expense report transactions for Federal Open Payments/Sunshine report, we " & _
"noticed that an incorrect expense type was selected for one or more of your meetings. On the following " & _
"report, you selected an incorrect expense type of " & _
"<b>Meals w/non HCPs out of office.</b> " & _
"It appears that there were HCPs present during the meeting(s)."
Msg = Msg & "<br/><br/>" & _
"Please make sure that going forward, you select a correct expense type for all meetings with HCPs " & _
"<b>(Example: Meal w/HCP out Office-Non-Promo).</b> " & _
"We need to ensure that we are reporting correct information. Please note that future violations could result " & _
"in notification to your manager. If you have any questions, please let me know."
Msg = Msg & "<br/><br/><b>Expense Report Details:</b>" & _
"<br/><br/><b>Report Name: </b>" & _
Rname
Msg = Msg & "<br/><br/><b>Date: </b>" & _
Rdate
Msg = Msg & "<br/><br/><b>Amount: </b>" & _
Ramount
Msg = Msg & "<br/><br/><b>Vendor Name: </b>" & _
Vendor
Msg = Msg & "<br/><br/><b>HCP Name(s): </b>" & _
CHCPName
Msg = Msg & "<br/><br/>Regards<br/><br/>Sunil Kumar<br/>Manager<br/>" & _
"[email protected]<br/>+1(817)615-2333"
getMessageBody = Msg
End Function
如果唯一改变的是电子邮件,您可以添加列到一个数组或字典来帮助您删除重复的地址的方式。然后使用'ForArr'中的每个地址来生成电子邮件。 –