【发布时间】:2017-07-09 19:38:12
【问题描述】:
我必须创建一个 VBA 来发送自动电子邮件(电子邮件正文将收件人链接到他负责的特定项目)。我遇到的问题是某个收件人(即放置在“TO”中)可以负责更多任务。我正在使用的 VBA 向每个任务发送电子邮件(即使该人负责更多)。如果发送包含所有任务的电子邮件大于 1,我该怎么做才能通过收件人计数。我真的需要你的帮助。
<PRE>Sub SendEMail()
Dim OutApp As Object
Dim OutMail As Object
Dim lastRow As Long
Dim Ebody As String
lastRow = ThisWorkbook.Worksheets("Sheet1").Cells(Rows.Count, "B").End(xlUp).Row
For i = 2 To lastRow
Ebody = "<FONT SIZE = 4 name = Arial>" & "Dear " & Cells(i, "A").Value
& "<br>" _
& "<br>" _
& "Please note that the below mentioned projectd are in scope for reporting." & "<br>" _
& "<br>" _
& Cells(i, "C").Value & " - " & Cells(i, "E").Value & "<br>" _
& "xxxxx will investigate and action your notification according to priority and to ensure public safety." & "<br>" _
& "For further information, please phone xxxxx on 6111 and quote reference number:" & "<br>" _
& "Your original report can be seen below:" & "</Font>" & "<br>" _
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = Cells(i, "B").Value
.Cc = Cells(i, "D").Value
.Subject = "Your Registration Code"
.HtmlBody = Ebody
.Attachments.Add "C:\Test\Document.docx"
.Attachments.Add "C:\Test\Document1.docx"
.SentOnBehalfOfName = "Financial@yahoo.com"
.Display
End With
Next
End Sub </pre>
【问题讨论】:
-
我在这种情况下所做的,是重组数据,使每一行表示 1 封电子邮件,如果有重复的电子邮件,我将额外的任务添加到单元格中并用逗号分隔数据。然后我执行 instr(",", cell)> 0,然后将任务调整到一个数组中,然后在发送之前将它们循环到电子邮件中。
-
非常感谢。如果您能帮助我编写代码,那将非常有用。
-
好的,但我是否正确理解了您的问题?
-
当前表结构:第 1 列:电子邮件地址,第 2 列:项目编号(即任务),第 3 列:项目名称。有时电子邮件地址重复。在这种情况下,VBA 只需为各个项目发送一封电子邮件(即第二列)。项目编号和项目名称必须插入电子邮件正文中的不同行(即像项目符号点一样)。
-
是的,因此您需要创建一个附加列,如果该行不重复,则该列将为空白,而如果该行不为空白,则它应该包含两个项目,并用逗号分隔这些值。