使用宏向特定收件人发送电子邮件

时间:2018-05-02 05:54:36

标签: excel vba excel-vba email

我正在尝试向特定收件人发送电子邮件提醒,其关联的用户ID存储在Excel工作表中。如果他们尚未完成调查,电子邮件将仅发送给收件人。 (将细胞(iCounter,3))

MailDest是所有收件人的所在地'电子邮件位于。 DGName包含收件人的所有关联用户标识。一个收件人可以拥有多个用户ID。

问题是收件人的所有用户ID(DGName)都是在发送给他们的每封电子邮件中生成的。

我应该如何编辑我的代码,以便将电子邮件发送给收件人相关的电子邮件及其用户ID?

修改 以下是代码:

Sub SendReminderMail()

Dim OutLookApp As Object    
Dim OutLookMailItem As Object    
Dim iCounter As Integer    
Dim MailDest As String    
Dim DGName As String

Set OutLookApp = CreateObject("Outlook.application")    
Set OutLookMailItem = OutLookApp.CreateItem(0)

With OutLookMailItem    
    MailDest = ""    
    For iCounter = 1 To WorksheetFunction.CountA(Columns(5))

        If MailDest = "" And Cells(iCounter, 3) = "" Then
            MailDest = Cells(iCounter, 5).Value
            DGName = Cells(iCounter, 1).Value
        ElseIf MailDest <> "" And Cells(iCounter, 3) = "" Then
            MailDest = MailDest & ";" & Cells(iCounter, 5)
            DGName = DGName & ";" & Cells(iCounter, 1)
        End If

    Next iCounter

    .BCC = MailDest
    .Subject = "W2K16 update on Cloudworkplace Desktop Group"
    .HTMLBody = "Dear ~Whoever ," & "<br/><br/>" & DGName & "<br/><br/>" & " ~Message"
    .send

在此示例中,如果&#34;反馈&#34;列为空,它将触发宏以将提醒邮件发送给收件人。

Sample Excel Data

2 个答案:

答案 0 :(得分:1)

那么,此时您将1封电子邮件发送到多个地址(BCC)。因此,您需要更改代码以发送多封邮件(每封邮件一封邮件)。

因此,只需将代码发送到for循环中发送邮件:

With OutLookMailItem       
    For iCounter = 1 To WorksheetFunction.CountA(Columns(16))
        If Cells(iCounter, 14) = "" Then
            MailDest = Cells(iCounter, 16).Value
            DGName = Cells(iCounter, 12).Value
            .To = MailDest
            .Subject = "W2K16 update on Cloudworkplace Desktop Group"
            .HTMLBody = "Dear ~Whoever ," & "<br/><br/>" & DGName & "<br/><br/>" & " ~Message"
            .send
        End If
    Next iCounter

答案 1 :(得分:-1)

当我查看你的代码时,发现DGName变量需要在循环再次进入For 1st循环后生成空字符串

我在解决方案中写这个,因为无法写评论。 代码如下所示

Sub SendReminderMail()

Dim OutLookApp As Object
Dim OutLookMailItem As Object
Dim iCounter As Integer
Dim MailDest As String
Dim DGName As String

Set OutLookApp = CreateObject("Outlook.application")
Set OutLookMailItem = OutLookApp.CreateItem(0)

With OutLookMailItem
    MailDest = ""

    For iCounter = 1 To WorksheetFunction.CountA(Columns(16))
    DGName = "" 'see here

        If MailDest = "" And Cells(iCounter, 14) = "" Then
            MailDest = Cells(iCounter, 16).Value
            DGName = Cells(iCounter, 12).Value
        ElseIf MailDest <> "" And Cells(iCounter, 14) = "" Then
            MailDest = MailDest & ";" & Cells(iCounter, 16)
            DGName = DGName & ";" & Cells(iCounter, 12)
        End If

    Next iCounter

    .BCC = MailDest
    .Subject = "W2K16 update on Cloudworkplace Desktop Group"
    .HTMLBody = "Dear ~Whoever ," & "<br/><br/>" & DGName & "<br/><br/>" & " ~Message"
    .send