如何发送2个不同的自动Outlook电子邮件从同一个Excel工作表时7天到期日

fhity93d  于 2023-02-25  发布在  其他
关注(0)|答案(1)|浏览(141)

我是一个新手,但一直在寻找其他解决方案来解决我的问题,所以我对Excel和VBA有些熟悉。我正在尝试找出如何在同事的工作还有7天就要到期时自动通知他们。同一个工作表上有两个单独的表(每个同事一个工作表)。我如何让VBA根据同一个工作表上的两个不同的表发送电子邮件?
例如,Bill有即将到期的治疗计划,他也有即将到期的评估,治疗计划在一个表中,评估在另一个表中,但它们在同一张纸上(Sheet2 Bill)。我希望在治疗计划离到期日还有7天时自动向Bill发送一封电子邮件。我希望在评估离到期日还有7天时自动向Bill发送另一封电子邮件。
这是目前为止我拥有的代码。我没有看到任何错误消息,但我也没有收到任何电子邮件。
谢谢你的帮助!
客户端名称是B列。

Sub email()
Dim r As Range, cell As Range
Dim ws As Worksheet
Dim Mail_Object As Object, Mail_Single As Object
Dim Email_Subject As String, Email_Send_From As String, Email_Send_To As String, _
    Email_Cc As String, Email_Bcc As String, Email_Body As String

Set ws = ThisWorkbook.Worksheets("Sheet2 (Bill)")
Set r = ws.Range("F5:F12")
Set Mail_Object = CreateObject("Outlook.Application")

For Each cell In r
    If cell.Value <= (Date + 7) And cell.Value >= (Date) Then

        Email_Subject = "Treatment plan is due soon"
        Email_Send_From = "blahblah@blahblah.blah"
        Email_Send_To = "blahblah@blahblah.blah"
        Email_Body = "This is an automated reminder that you have a treatment plan due within the next 7 days."

        On Error GoTo debugs
        Set Mail_Single = Mail_Object.CreateItem(0)

        With Mail_Single
            .Subject = Email_Subject
            .To = Email_Send_To
            .Body = Email_Body
            .send
        End With

    End If
    
Next cell
Sub email()
Dim r As Range, cell As Range
Dim ws As Worksheet
Dim Mail_Object As Object, Mail_Single As Object
Dim Email_Subject As String, Email_Send_From As String, Email_Send_To As String, _
    Email_Cc As String, Email_Bcc As String, Email_Body As String

Set ws = ThisWorkbook.Worksheets("Sheet2 (Bill)")
Set r = ws.Range("F19:F26")
Set Mail_Object = CreateObject("Outlook.Application")

For Each cell In r
    If cell.Value <= (Date + 7) And cell.Value >= (Date) Then

        Email_Subject = "Treatment plan is due soon"
        Email_Send_From = "blahblah@blahblah.blah"
        Email_Send_To = "blahblah@blahblah.blah"
        Email_Body = "This is an automated reminder that you have a treatment plan due within the next 7 days."

        On Error GoTo debugs
        Set Mail_Single = Mail_Object.CreateItem(0)

        With Mail_Single
            .Subject = Email_Subject
            .To = Email_Send_To
            .Body = Email_Body
            .send
        End With
        End With

    End If

debugs: If Err.Description <> "" Then MsgBox Err.Description

End Sub
mum43rcc

mum43rcc1#

1.打开Excel工作簿,然后按ALT + F11打开VBA编辑器。
1.转到插入〉模块并粘贴以下代码:

Sub SendEmails()
Dim OutApp As Object
Dim OutMail1 As Object
Dim OutMail2 As Object
Dim cel As Range
Dim dueDate As Date
Dim daysLeft As Long

Set OutApp = CreateObject("Outlook.Application")

'Change the email addresses and subject lines as needed
Set OutMail1 = OutApp.CreateItem(0)
With OutMail1
    .To = "email1@example.com"
    .Subject = "Reminder 1"
    .Body = "This is reminder 1."
End With

Set OutMail2 = OutApp.CreateItem(0)
With OutMail2
    .To = "email2@example.com"
    .Subject = "Reminder 2"
    .Body = "This is reminder 2."
End With

'Change "A2:A10" to the range of cells that contain the due dates
For Each cel In Range("A2:A10")
    If IsDate(cel.Value) Then
        dueDate = cel.Value
        daysLeft = DateDiff("d", Now(), dueDate)
        
        'Change "7" to the number of days before the due date to send the reminder
        If daysLeft = 7 Then
            OutMail1.Send
            OutMail2.Send
        End If
    End If
Next cel

Set OutMail1 = Nothing
Set OutMail2 = Nothing
Set OutApp = Nothing
End Sub

相关问题