The Site Doctor Blog

Footprints in the snow of a warped mind

< Back to Blog

Delay sending an email until the next working day

I know there are a fair few people that have automatic delays on their email to avoid sending emails too quickly but for some time now I've wanted to restrict emails so they're only send during office hours -to avoid getting into email conversations on the weekend.

I was looking around and I came across this VBA script to delay emails which was a good start but it had a few flaws to it.

Outlook being Outlook, it's a little tricky to install a VBA script so it's probably easiest to check Google for that side of things but here's an adapted version of the script which will delay sending emails until 9am the next working day.

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
    SendTime = " 09:00:00"              ' Time to deliver delayed mail (6AM)
    DelayMailBefore = 9                 ' Delay mail sent before 5:00AM
    DelayMailAfter = 17                 ' Delay mail sent after 8:59PM
    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"
        End Sub
    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
Author: Tim on

Liked this post? Got a suggestion? Leave a comment