0
我想要一個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
如果唯一改變的是電子郵件,您可以添加列到一個數組或字典來幫助您刪除重複的地址的方式。然後使用'ForArr'中的每個地址來生成電子郵件。 –