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.
Similar threads
Thread starter Title Forum Replies Date
Diane Poremsky To delay sending a message in Outlook Using Outlook 0
Diane Poremsky To delay sending a message in Outlook Using Outlook 1
M How to forward a message and delay sending for up to 30 days Using Outlook 8
T Exchange Server - Mail Delay Stamping Exchange Server Administration 0
R Outlook 365 VBA AUTO SEND WITH DELAY FOR EACH EMAIL Outlook VBA and Custom Forms 0
L Modifying VBA script to delay running macro Outlook VBA and Custom Forms 3
J Diane's Delay Delivery at Specific Times Questions Outlook VBA and Custom Forms 7
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 Using Outlook 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
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
W Outlook 2021 Hanging on "Sending Emails" Using Outlook 3
Fozzie Bear Calendar Events created on iPhone have suddenly start sending invitations to attendees Using Outlook 2
D Prompt to prefix subject line whenever sending an email Outlook VBA and Custom Forms 3
P default font when sending email from browser Using Outlook 1
M Messages Intermittently Dont Arrive In Sent Items After Sending Successfully Using Outlook 4
O Outlook on Android: after sharing / sending a news article, draft remains open. Why? Using Outlook 1
P Sending email from outlook IMAP to GMAIL where embedded images are added as attachment Using Outlook 1
V Form data not sending for some users Outlook VBA and Custom Forms 2
L isn't there an OL add-on that flags addressee before sending Using Outlook 3
R Microsoft Outlook 2016 - Gmail not sending, asks for password for SMTP, tried different ports Using Outlook 23
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
A Script to fetch data from mails in restricted collection and sending them to excel Using Outlook 1
R Sending emails via Outlook XP, from Windows 10 File Explorer Using Outlook 1
L Creating drafts when I thought I was sending Using Outlook 1

Similar threads

Back
Top