【问题标题】:VBA Excel Sending Individual EmailsVBA Excel 发送个人电子邮件
【发布时间】:2016-02-11 16:36:08
【问题描述】:

我有以下代码,对我来说非常好用。 它整理“名称”列(第 I 列)中的名称,以根据其他单元格(L、K)中的条件生成电子邮件列表,并生成包含工作表中一些内容的消息正文,因此我可以将其发送到列表的收件人。

我现在需要通过单独的电子邮件发送它,而不是发送给每个人的一封电子邮件。我现在可以通过使用名称过滤列 I 来做到这一点,但是如果有 100 个名称,那就有点烦人了……我可以更改代码以使其为收件人生成单独的电子邮件吗?

附言欣赏代码可能有点混乱/没有优化,但我是新手......谢谢

Sub SendEmail()

    Dim OutlookApp
    Dim MItem
    Dim cell As Range
    Dim Subj As String
    Dim EmailAddr As String
    Dim Recipient As String
    Dim Msg As String
    Dim Projects As String
    Dim ProjectsMsg As String


    'Create Outlook object
    Set OutlookApp = CreateObject("Outlook.Application")
    Set MItem = OutlookApp.CreateItem(0)
    'Loop through the rows
    For Each cell In Columns("I").Cells.SpecialCells(xlCellTypeVisible)
     If cell.Value <> "" And _
           (Cells(cell.Row, "L").Value) = "No" And (Cells(cell.Row, "K").Value) <> "Yes" Then
            'first build email address
            EmailAddr = LCase$(Replace(cell.Value, " ", ".")) & "@company.com"
            'then check if it is in Recipient List build, if not, add it, otherwise ignore
            If InStr(1, Recipient, EmailAddr) = 0 Then Recipient = Recipient & ";" & EmailAddr
        End If
    Next


    Recipient = Mid(Recipient, 2) 

For Each cell In Columns("C").Cells.SpecialCells(xlCellTypeVisible)
     If cell.Value <> "" And _
           (Cells(cell.Row, "L").Value) = "No" And (Cells(cell.Row, "K").Value) <> "Yes" And _
           (Cells(cell.Row, "I").Value) <> "" Then
             Projects = vbCrLf & "Document: " & Cells(cell.Row, "B").Value & "; " & Cells(cell.Row, "C").Value & "; " & "Rev " & Cells(cell.Row, "G").Value & "; " & Cells(cell.Row, "I").Value
             If InStr(1, ProjectsMsg, Projects) = 0 Then ProjectsMsg = ProjectsMsg & Projects & vbCrLf
        End If
Next
    Msg = "Please review the following: " & ProjectMsg
    Subj = "Outstanding Documents to be Reviewed"
    'Create Mail Item and view before sending
    Set MItem = OutlookApp.CreateItem(olMailItem)
    With MItem
        .To = Recipient 'full recipient list
        .Subject = Subj
        .Body = Msg
        .display
    End With

End Sub

【问题讨论】:

    标签: vba excel email


    【解决方案1】:

    我认为您希望做的是将收件人列表放入电子邮件中,然后让电子邮件为每个人生成不同的电子邮件。它不太像这样工作。

    相反,移动代码以在循环中生成电子邮件,以便您每次生成新电子邮件并发送它。首先创建项目消息和主题,以便他们准备好接收电子邮件。

    Sub SendEmail()
    
    Dim OutlookApp
    Dim MItem
    Dim cell As Range
    Dim Subj As String
    Dim EmailAddr As String
    Dim PriorRecipients As String
    Dim Msg As String
    Dim Projects As String
    Dim ProjectsMsg As String
    
    
    'Create Outlook object
    Set OutlookApp = CreateObject("Outlook.Application")
    PriorRecipients = ""
    
    'First create the body for the message
     For Each cell In Columns("C").Cells.SpecialCells(xlCellTypeVisible)
          If cell.Value <> "" And _
           (Cells(cell.Row, "L").Value) = "No" And (Cells(cell.Row, "K").Value) <> "Yes" And _
           (Cells(cell.Row, "I").Value) <> "" Then
                  Projects = vbCrLf & "Document: " & Cells(cell.Row, "B").Value & "; " & Cells(cell.Row, "C").Value & "; " & "Rev " & Cells(cell.Row, "G").Value & "; " & Cells(cell.Row, "I").Value
                  If InStr(1, ProjectsMsg, Projects) = 0 Then ProjectsMsg = ProjectsMsg & Projects & vbCrLf
            End If
     Next
    
    Msg = "Please review the following: " & ProjectMsg
    Subj = "Outstanding Documents to be Reviewed"
    
    'Loop through each person and send email if they haven't already received one.
    For Each cell In Columns("I").Cells.SpecialCells(xlCellTypeVisible)
         If cell.Value <> "" And _
           (Cells(cell.Row, "L").Value) = "No" And (Cells(cell.Row, "K").Value) <> "Yes" Then
            'first build email address
            EmailAddr = LCase$(Replace(cell.Value, " ", ".")) & "@company.com"
            'then check if it is in Recipient List build, if not, add it, otherwise ignore
             'If the recipient has already received an email, skip
             If InStr(1, PriorRecipients, EmailAddr) <> 0 Then 
                 GoTo NextRecipient              
             End If
    
             PriorRecipients = PriorRecipients & ";" & EmailAddr
             'Create Mail Item and view before sending
             Set MItem = OutlookApp.CreateItem(olMailItem)
             With MItem
                  .To = EmailAddr 'single email address
                  .Subject = Subj
                  .Body = Msg
                  .display 
                  'This will show for EVERY person.  Skip this and change to .send to just send without showing the email.
             End With
          End If
     NextRecipient:
    
     Next
    
    End Sub
    

    【讨论】:

    • 是的,这回答了这个问题......一个快速而肮脏的答案,其中一些(但不是全部)将在'bcc'中放入一个名称数组:相同'blind复制的电子邮件,每个都有一个收件人,是从一个文档生成的。我将其作为评论提供,因为它的常见用例是发送给多个组的电子邮件,其中服务器太愚蠢而无法检测重叠的组成员并确保每人只发送一封电子邮件;反过来,这涉及扩展组并在收件人姓名列表上运行“DISTINCT VALUES”操作。
    • @Nile,所以您将列表放在密件抄送字段中,展开名称,然后从密件抄送字段中获取名称并生成新电子邮件,给每个不同的人发一封?这是一个很好的解决方案,肯定适用于 OPs 问题。
    • @OpiesDad 谢谢,这行得通。任何方式我都可以让它遍历 C 列中的项目,这样我每个电子邮件/收件人就有几个项目..现在它忽略了这一点并列出了所有符合条件的项目......
    • @warfo 您想发送一封电子邮件,其中只包含属于收件人的项目?然后将电子邮件消息的创建移动到每个人的循环中(在“Set MItem =”行的正上方,然后检查我的列是否包含任何文本,而不是检查它是否包含您要发送的人的姓名电子邮件发给。学习编程需要反复试验。首先弄清楚你想做什么,然后弄清楚如何让计算机在逻辑上做到这一点。尝试不同的事情,最终你会学会的。
    • @OpiesDad 知道了,感谢您的帮助
    猜你喜欢
    • 2014-08-17
    • 2022-09-24
    • 2023-03-11
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    相关资源
    最近更新 更多