当任务标记为已完成时,如何停止发送电子邮件的代码?

时间:2019-04-01 19:06:13

标签: excel vba

以下代码可以在到期日或之前的7天发送电子邮件,但要使其运行,需要将其分配给Excel中的按钮。打开文件后,我希望它自动运行代码并将电子邮件发送给那些即将执行或过期的任务。它需要停止发送已标记为“已完成”任务的电子邮件。

<div class="three_separation">
  <div class='grades_dashboard_box'>
    <div class="vertical_center">
      <h1 class="grade_display" id="grade_display_best">2.3</h1>
      <div class="vertical_center grade_info" style="margin-left: 100px;">
        <p style="font-size: 15px;">Beste Durchschnittsnote</p>
        <p id="grade_display_best_sub" style="font-size: 20px;">Biologie</p>
      </div>
    </div>
  </div>
  <div class='grades_dashboard_box'>
    <div class="vertical_center">
      <h1 class="grade_display" id="grade_display_average">13.70</h1>
      <div class="vertical_center grade_info" style="margin-left: 100px;">
        <p style="font-size: 15px;">Durchschnittsnote</p>
      </div>
    </div>
  </div>
  <div class='grades_dashboard_box'>
    <div class="vertical_center">
      <h1 class="grade_display" id="grade_display_worst">3.4</h1>
      <div class="vertical_center grade_info" style="margin-left: 100px;">
        <p style="font-size: 15px;">Schlechteste Durchschnittsnote</p>
        <p id="grade_display_worst_sub" style="font-size: 20px;">Deutsch</p>
      </div>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

在循环内,放置一条if语句...修复单元格引用:

For i = 2 To lRow
    If Cells(i,1).value <> "Completed" Then 'could also use Not Cells(i,1).value = "Completed"
        'all of your regular code
    End If
Next i

编辑1:

更新以使用您的代码:

For i = 2 To lRow
    If Cells(i,1).value <> "Completed" Then 'OPEN IT HERE
        If Cells(i, 5) <> "" Then
            toDate = Replace(Cells(i, 5), ".", "/")
            If Left(Cells(i, 18), 5) <> "Mail" And toDate - Date <= 7 Then
                Set OutApp = CreateObject("Outlook.Application")
                Set OutMail = OutApp.CreateItem(0)
                toList = Cells(i, 7)
                eSubject = "ACTION ITEM - " & Cells(i, 3) & " is due on " & Cells(i, 5)
                eBody = "NOTICE for " & Cells(i, 6) & vbCrLf & vbCrLf & "You have task(s)coming due or ones that are past due. Please complete your tasks as soon as possible, then notify the Quality Administrator when the task is complete."
                On Error Resume Next
                With OutMail
                    .To = toList
                    .CC = ""
                    .BCC = ""
                    .Subject = eSubject
                    .Body = eBody
                    .bodyformat = 1
                    '.Display
                    .Display
                End With
                On Error GoTo 0
                Set OutMail = Nothing
                Set OutApp = Nothing
                Cells(i, 9) = "Mail Sent " & Date + Time
            End If
        End If
    End If 'CLOSE IT HERE
Next i

使用现有的If语句的另一种方法:

For i = 2 To lRow
    If Cells(i, 5) <> "" Or Cells(i,1).value <> "Completed" Then
        toDate = Replace(Cells(i, 5), ".", "/")
        If Left(Cells(i, 18), 5) <> "Mail" And toDate - Date <= 7 Then
            Set OutApp = CreateObject("Outlook.Application")
            Set OutMail = OutApp.CreateItem(0)
            toList = Cells(i, 7)
            eSubject = "ACTION ITEM - " & Cells(i, 3) & " is due on " & Cells(i, 5)
            eBody = "NOTICE for " & Cells(i, 6) & vbCrLf & vbCrLf & "You have task(s)coming due or ones that are past due. Please complete your tasks as soon as possible, then notify the Quality Administrator when the task is complete."
            On Error Resume Next
            With OutMail
                .To = toList
                .CC = ""
                .BCC = ""
                .Subject = eSubject
                .Body = eBody
                .bodyformat = 1
                '.Display
                .Display
            End With
            On Error GoTo 0
            Set OutMail = Nothing
            Set OutApp = Nothing
            Cells(i, 9) = "Mail Sent " & Date + Time
        End If
    End If
Next i