Outlook VBS - Automatically delay sending evening & weekend emails until 0700 the following weekday (script not quite right)

RichCowell

Board Regular
Joined
Dec 5, 2013
Messages
121
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I've got a script for Outlook that *should* delay sending emails Mon-Fri after 1800, and any time at the weekend, until 0700 the next weekday morning.

It seems to work Mon-Thu, but Fri-Sat it just sends it the following morning rather than waiting until Monday morning.

Can anyone help identify/correct the issues? I'm a complete novice with VBS & Outlook, but I'm trying to promote mental health and wellbeing, and trying to minimise the work I put on others outside working hours.

Code:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim dayname As String

' If after 6PM
  If Now() > DateSerial(Year(Now), Month(Now), Day(Now)) + #5:59:00 PM# Then
    sendat = DateSerial(Year(Now), Month(Now), Day(Now) + 1) + #7:00:00 AM#
' If before 7AM
  ElseIf Now() < DateSerial(Year(Now), Month(Now), Day(Now)) + #6:59:00 AM# Then
    sendat = DateSerial(Year(Now), Month(Now), Day(Now)) + #7:00:00 AM#
' We'll test the date of all messages
 ElseIf WeekdayName(Weekday(Now())) = "Saturday" Or WeekdayName(Weekday(Now())) = "Sunday" Then
   ' this will be changed by the next part if a weekend
   sendat = DateSerial(Year(Now), Month(Now), Day(Now)) + #11:00:00 PM#
 End If

dayname = WeekdayName(Weekday(sendat))

Select Case dayname
Case "Saturday"
    sendat = DateSerial(Year(Now), Month(Now), Day(Now) + 2) + #7:00:00 AM#
Case "Sunday"
    sendat = DateSerial(Year(Now), Month(Now), Day(Now) + 1) + #7:00:00 AM#
End Select
    Item.DeferredDeliveryTime = sendat
Debug.Print Now(), dayname, sendat

End Sub

Thanks,

Rick
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Currently when I run your code (5.53 PM CET) none of the conditions are met, so the sendat variable stays empty.
Therefore dayname results in "Sunday" because of the result of Weekday(0), although todays day is Monday.
So don't test on Saturday or Sunday in the last ElseIf clause, do test on Friday and ad a final Else clause where sendat obtains a valid date/time value.

Btw, this part in your code
VBA Code:
DateSerial(Year(Now), Month(Now), Day(Now)

may be replaced with
VBA Code:
Date
 
Upvote 0
.. and in the end it will look like this:

VBA Code:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

    Const DELAYDAYSFROM As String = "3.Friday, 2.Saturday, 1.Sunday"

    Dim AddDays As Long, Pos As Long, SendTime As Date

    ' assume a delayed send
    SendTime = #7:00:00 AM#

    ' assume monday, tuesday, wednesday or thursday
    If Time < #6:59:00 AM# Then
        AddDays = 0
    ElseIf Time > #5:59:00 PM# Then
        AddDays = 1
    Else
        ' send immediately
        AddDays = -1
    End If
   
    ' check on weekend
    Pos = InStr(1, DELAYDAYSFROM, VBA.Format$(Date, "dddd"), vbTextCompare)
    If Pos > 0 Then

        ' possible delay until monday
        AddDays = CLng(Mid$(DELAYDAYSFROM, Pos - 2, 1))
       
        ' Friday?
        If AddDays = 3 Then

            'Friday before 7.00 AM?
            If Time < #6:59:00 AM# Then
                ' yep, postpone to 7.00 AM same day
                AddDays = 0

            'Friday after 6.00 PM?
            ElseIf Time > #5:59:00 PM# Then
                ' yep, postpone to monday

            Else
                ' Friday during office hours, send immediately
                AddDays = -1
            End If
        End If
    End If

    If Not AddDays = -1 Then
        Item.DeferredDeliveryTime = Date + AddDays + SendTime
    End If
End Sub
 
Upvote 0
Thanks for that @GWteB - that's quite a different code than the original - it's no wonder I couldn't work it out!
I'll get that tested over the next week... I'm planning on writing a blog post about it when I know it works - can I thank you and acknowledge @GWteB from mrexcel.com?
 
Upvote 0
You are welcome. I did not test it but I am quite sure it will work. If it doesn't work let me know, I'll keep watching ...
 
Upvote 0
@GWteB - I've just had to remove the 32bit version off Office and install the 64bit (I'm using Office 365 bought through that rather than Office 2019 etc. - still the full client, but a different version)...
Do you know if there's likely to be a difference with that script in the 64bit version? Whenever I open Outlook now it gives me a warning about the Macro, and when I go to sign it again, it crashes Outlook completely without opening the Digital Signatures window...
 
Upvote 0
My code is just "plain" VBA. Afaik it should work on both 32 and 64-bit Office. I'm on 2013 32-bit and not able to test on 365 64-bit.
The warning you got may find it's cause in the current settings of Outlook's Trust Center which may have changed after the upgrade.
I don't use digital signatures so I can't tell whether Outlook argues about the current certificates (resulting in the crash you experienced) or not.
Pity I can't help you with your current issue. Perhaps another forum member who does have Outlook 365 64-bit is willing to step in ...
 
Upvote 0
Thanks - I got to the bottom of it by deleting the VBA project, closing Outlook, deleting the OTM file in the %AppData%\Microsoft\Outlook folder, then doing it from scratch again... Looks like it's working again now... :-)
 
Upvote 0
Quick follow up... is it possible to add an exception/bypass to this based on a keyword e.g. putting "SendNow" in the subject (or snuck in the body - I could hide it in a tiny font then)?
 
Upvote 0
Okay, how about ...

VBA Code:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

    Const DELAYDAYSFROM As String = "3.Friday, 2.Saturday, 1.Sunday"
    Const KEYWORD       As String = "SendNow"                           ' <<<< change to suit

    Dim AddDays As Long, Pos As Long, SendTime As Date


    If InStr(1, Item.Body, KEYWORD, vbTextCompare) Then                 ' <<<< choise 1: keyword in body
'    If InStr(1, Item.Subject, KEYWORD, vbTextCompare) Then              ' <<<< choise 2: keyword in subject

        ' send immediately
        AddDays = -1

    Else

        ' assume a delayed send
        SendTime = #7:00:00 AM#
    
        ' assume monday, tuesday, wednesday or thursday
        If Time < #6:59:00 AM# Then
            AddDays = 0
        ElseIf Time > #5:59:00 PM# Then
            AddDays = 1
        Else
            ' send immediately
            AddDays = -1
        End If
       
        ' check on weekend
        Pos = InStr(1, DELAYDAYSFROM, VBA.Format$(Date, "dddd"), vbTextCompare)
        If Pos > 0 Then
    
            ' possible delay until monday
            AddDays = CLng(Mid$(DELAYDAYSFROM, Pos - 2, 1))
           
            ' Friday?
            If AddDays = 3 Then
    
                'Friday before 7.00 AM?
                If Time < #6:59:00 AM# Then
                    ' yep, postpone to 7.00 AM same day
                    AddDays = 0
    
                'Friday after 6.00 PM?
                ElseIf Time > #5:59:00 PM# Then
                    ' yep, postpone to monday
    
                Else
                    ' Friday during office hours, send immediately
                    AddDays = -1
                End If
            End If
        End If
    End If

    If Not AddDays = -1 Then
        Item.DeferredDeliveryTime = Date + AddDays + SendTime
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,862
Messages
6,181,466
Members
453,045
Latest member
Abraxas_X

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top