打开文件时如何停止发送电子邮件的次数倍增

时间:2019-04-04 15:43:03

标签: excel vba

我需要代码帮助,每天只发送一次电子邮件。

打开文件时,代码将设置为自动发送电子邮件,并基于截止日期。但是,该文件在一整天内可以多次打开。我需要它每天只发送一次电子邮件(第一次打开文件),但我不知道如何正确编码。

    For i = 2 To lRow

    If Cells(i, 8).Value <> "Completed" Then 
        If Cells(i, 2) <> "" 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 & "This is a reminder that you have task(s) that are 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
                    .Send
                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 
    Next i

1 个答案:

答案 0 :(得分:0)

赞:

For i = 2 To lRow
    If Cells(i, 8).Value <> "Completed" And _
       Cells(i, 2) <> "" And _
       Cells(i, 9) <> Date Then

            'send the mail
            Cells(i, 9) = Date '<<< store the date sent

    End If
Next i