Delay sending an email until the next working day (public holidays)

Not open for further replies.


New Member
Outlook version
Outlook 2016 64 bit
Email Account
Exchange Server 2013

The following code works very well to delay sending messages in Outlook when it's late (working days) en it's the weekend. The sent messages are delayed until the next working day but I would like also to delay sending messages until the next working day when I work during a public holiday!;-)

I imported public holidays in my Outlook calendar :
Add holidays to your calendar in Outlook for Windows - Outlook

Is it possible to modify the code like this : if appointment with the category "Holiday", delay sending messages until the next working day.

Thank you in advance !

The code :
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    On Error GoTo ErrHand               ' Error Handling
    Dim objMailItem As MailItem         ' Object to hold mail item
    Dim SendDate As String              ' The date to send delayed mail
    Dim SendTime As String              ' The time to send delayed mail
    Dim DelayMailAfter As Integer       ' Latest hour mail is sent live
    Dim DelayMailBefore As Integer      ' Earliest hour to send mail live
    Dim DelayForDays As Integer         ' The number of days to delay the email for
    Dim MailIsDelayed As Boolean        ' Set if the mail will be delayed
    Dim NoDeferredDelivery As String    ' Value if deferred delivery is disabled
    Dim PublicHoliday As CalItem
    SendTime = " 06:00:00"              ' Time to deliver delayed mail (06:00)
    DelayMailBefore = 5                 ' Delay mail sent before 05:00
    DelayMailAfter = 19                 ' Delay mail sent after 19:00
    DelayForDays = 0                    ' Default to sending emails the same day
    MailIsDelayed = True                ' We assume it's delayed by default
    NoDeferredDelivery = "1/1/4501"     ' Magic number Outlook uses for "delay mail box isn't checked"
    Set objMailItem = Item ' Outlook.ActiveInspector.CurrentItem
    ' Check and make sure current item is an unsent message
    If objMailItem.Sent = True Then
        Err.Raise 9000
    End If
    If Weekday(Date, vbMonday) = 6 Then ' Today is Saturday, delay mail two days
        DelayForDays = 2
    ElseIf Weekday(Date, vbMonday) = 7 Then ' Today is Sunday, delay mail one day
        DelayForDays = 1
    Else ' Currently a weekday
        If DatePart("h", Now) < DelayMailBefore Then ' It's early morning - delay it
            DelayForDays = 0
        ElseIf DatePart("h", Now) > DelayMailAfter Then ' It's late night - delay it until tomorrow morning
            If Weekday(Date, vbMonday) = 5 Then ' Today is Friday, delay mail until Monday
                DelayForDays = 3
                DelayForDays = 1
            End If
            MailIsDelayed = False
        End If
    End If
    If MailIsDelayed And objMailItem.DeferredDeliveryTime = NoDeferredDelivery Then
        Dim NewSendDate As Date
        NewSendDate = (Date + DelayForDays) & SendTime
        ans = MsgBox("Out of hours - do you want to delay mail until " & NewSendDate & "?", vbYesNo, "Delay out of hours mail?")
        If ans = vbYes Then
            objMailItem.DeferredDeliveryTime = NewSendDate
            objMailItem.DeferredDeliveryTime = NoDeferredDelivery
        End If
    End If
    Exit Sub
    ' Handle well-known errors with message
    ' Other errors, just tell the user
    If Err.Number = 13 Then
        ' No current item or current item isn't a mail message
        ' MsgBox "Future delivery can only be set on mail items", vbOKOnly, "Not a mail item"
    ElseIf Err.Number = 9000 Then
        ' The active message has already been sent
        MsgBox "Please run this macro from an unsent mail item", vbOKOnly, "Not an unsent mail item"
        MsgBox "An error has occured on line " & Erl & _
                ", with a description: " & Err.Description & _
                ", and an error number " & Err.Number
    End If
End Sub
Not open for further replies.