VBA发送不同时间的群发邮件

时间:2015-05-15 15:20:55

标签: excel vba

我倾向于做的是我有一份包含工作人员名单的excel表格,我想发送一份包含excel表格详细信息的提醒。我正在尝试使用DefferdDeliverytime选项发送电子邮件,以便将电子邮件停放在发件箱中并在以后发送。使用此代码,我可以发送消息,但我不知道该怎么做根据excel表格添加代码以发送和发送具有特定发送时间的电子邮件。可以任何人为我添加一个代码,使其工作。

Sub Mail_To_Friends()
    'Below Loop can be changed to While Loop or increase the limit (10) if your list has more than 10 mail ids
    Dim SendTo As String
    Dim ToMSg As String
    Dim DueDate As String
    Dim AwardN As String
    Dim SendTime As String

    For i = 2 To 10
        SendTo = ThisWorkbook.Sheets(1).Cells(i, 22)
        If SendTo <> "" Then
            ToMSg = ThisWorkbook.Sheets(1).Cells(i, 15)
            DueDate = ThisWorkbook.Sheets(1).Cells(i, 14)
            AwardN = ThisWorkbook.Sheets(1).Cells(i, 1)
            SendTime = ThisWorkbook.Sheets(1).Cells(i, 22)
            Send_Mail SendTo, ToMSg, DueDate, AwardN, SendTime
        End If
    Next i
End Sub
Sub Send_Mail(SendTo As String, ToMSg As String, DueDate As String, AwardN As String, SendTime As String)
    Dim OutlookApp As Object
    Dim OutlookMail As Object

    Set OutlookApp = CreateObject("Outlook.Application")
    Set OutlookMail = OutlookApp.CreateItem(0)

    With OutlookMail
        .To = SendTo
        .CC = ""
        .BCC = ""
        .Subject = "draft report is due"
        .Body = "Dear " & ToMSg & vbNewLine & vbNewLine & "The following report is due to member on " & DueDate & vbNewLine & vbNewLine & "Award Name: " & AwardN
        .Display 'or just put .Send to directly send the mail instead of display
    End With

    Set OutlookMail = Nothing
    Set OutlookApp = Nothing
End Sub

1 个答案:

答案 0 :(得分:1)

这就像在MailItem上设置DeferredDeliveryTime属性一样简单。

With OutlookMail
    .To = SendTo
    .DeferredDeliveryTime = SendTime
    '... insert rest of your code here
End With

要记住几点;首先,此属性是DateTime,您的SendTime是String。它应该 接受这个假设它是有效的格式,即。 18/05/2015。如果您没有指定时间,则默认为00:00:00。 其次,这会将您的发件箱中的项目保留在本地PC上。因此,您必须打开Outlook才能在指定时间实际发送电子邮件。