如何在打开文件时多次停止发送电子邮件

问题描述 投票:0回答:1

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

打开文件时,代码将设置为自动发送电子邮件,并基于截止日期。但是,此文件可以在一天中的多个时间打开。我需要它每天只发送一次电子邮件(第一次打开文件),但我无法弄清楚如何正确编码它。

    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
excel vba
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.