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

Status
Not open for further replies.

Xav1050

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

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 :
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
            Else
                DelayForDays = 1
            End If
        Else
            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
        Else
            objMailItem.DeferredDeliveryTime = NoDeferredDelivery
        End If
    End If
    Exit Sub
ErrHand:
    ' 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"
    Else
        MsgBox "An error has occured on line " & Erl & _
                ", with a description: " & Err.Description & _
                ", and an error number " & Err.Number
    End If
       
End Sub
 
Status
Not open for further replies.
Thread starter Similar threads Forum Replies Date
I 5 minute delay sending email from 2010 to 2003 Exchange Server Administration 11
Diane Poremsky To delay sending a message in Outlook New Slipstick.com Articles 0
Diane Poremsky To delay sending a message in Outlook New Slipstick.com Articles 1
M How to forward a message and delay sending for up to 30 days Using Outlook 8
J Delay sending emails in Office Outlook 2010 Using Outlook 2
J Diane's Delay Delivery at Specific Times Questions Outlook VBA and Custom Forms 5
K Delay Send and Mail Merge Outlook VBA and Custom Forms 4
D Delay Send not working Using Outlook 3
T Query About "Delay Delivery" Function Using Outlook 5
S Delay Delivery Macro Outlook VBA and Custom Forms 1
Diane Poremsky Delay Delivery of Messages Sent at Specific Times New Slipstick.com Articles 0
B Macro to delay email Outlook VBA and Custom Forms 0
G "Delay Delivery" Email fails to send if opened while in Outbox Using Outlook 4
D Delay Delivery: Visible to recipients? Using Outlook 1
Y All emails are received with delay on outlook 2010 Using Outlook 5
R Long delay for outgoing mail Using Outlook 2
P time delay between accessing Outlook file folders, up 20+ second delay Using Outlook 1
W Attachment delay issue for listing network files Using Outlook 1
I Long delay in switching folders since I installed IE 9 Beta Using Outlook 2
S 6 to 8 second delay in folders opening since installing a number of updates yesterday. Using Outlook 1
K Outlook 2007 Delay Delivery feature not working Using Outlook 1
L Delay / Send outgoing mail during certain hours Using Outlook 1
N Outlook 2007 and 2010 New Mail Delay Exchange Server Administration 1
P Delay delivery not working in Outlook Using Outlook 1
A E-mail receive delay in Exchange 2010 Exchange Server Administration 4
H Messages won't leave Outbox - sit in Date:None and cannot be sent (delay send becomes checked as wel Using Outlook 1
S Delivery delay set by default Using Outlook 2
B Outlook - Delay in loading messages in Outlook folder Using Outlook 2
L Receiving Mail Delay Using Outlook 10
K Delay after creating a room before Set-CalendarProcessing functions Exchange Server Administration 8
J Rule to Forward email but delay delivery 10 minutes Outlook VBA and Custom Forms 2
C Outlook 2007 Delay Delivery Problem Using Outlook 10
A Flag Message for Follow Up after sending Outlook VBA and Custom Forms 1
D Sending email from Office 365 alias in Outlook Using Outlook 3
E Change sending account depending on Subjectline Outlook VBA and Custom Forms 0
L unblocking attachments before sending Office 365 Advanced Protection Using Outlook 0
B Outlook 2003 email sending & receiving suddenly stopped working Using Outlook 3
R Warn before sending message Outlook VBA and Custom Forms 4
HarvMan Hotmail - Sending email is undeliverable Using Outlook 4
A Sending contact vcards sends older version instead of updated version Using Outlook 4
M McAllister Outlook stops Sending/Receiving/Synching after disconnecting remote desktop session Using Outlook 2
D Adding Enterprise Exchange Email Account to Outlook Prevents Sending via Outlook.com Account Using Outlook.com accounts in Outlook 10
B When sending an email, I am showing 2 of my address's Using Outlook 1
M Auto expand Distribution List Before Sending Email Outlook VBA and Custom Forms 1
A Check for words in subject header before sending email Outlook VBA and Custom Forms 4
O Run macro automatically at sending an email Using Outlook 11
W Sending To Wrong Account Using Outlook 15
H Select Specific Account When Sending Email, Based on Current Folder Outlook VBA and Custom Forms 1
M Help sending email but removing signature via VBA Outlook VBA and Custom Forms 5
M MsgBox when not sending from specified account Outlook VBA and Custom Forms 2
Similar threads


















































Top