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
 
The previous code definitely works - been trying it for about a week now, so thanks (again) for that!
I'll give this a whirl and see for the next week...
Thank you! I really should try to get into VBA a bit, but not got a clue where to start!
 
Upvote 0

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Forum statistics

Threads
1,223,371
Messages
6,171,689
Members
452,418
Latest member
kennettz

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