Delaying Email Sending (Outlook + VBA)
Many academics work on weekends. Often this feels like the only time to get some ‘peace and quiet’, giving us a chance to reduce stress and thin our inboxes. [Edit — I note that this should typically be an extra ordinary activity, or maybe you are flexibly working, if in doubt take some time to relax and refresh without the work.]
However, I realise that in dealing with my email I could now be adding stress to others by adding to their email! This is especially true when I am emailing students and colleagues and inadvertently promoting a 24/7 lifestyle of ‘always working’.
To avoid this, lets set up a simple method of delaying sending of emails until the next working day. This means that you can deal with your inbox in peace, while letting others deal with it the next day. The script I propose works with Outlook, and will enable you to choose whether you want this delay for every email set out of hours (i.e. you can send off something urgent if you need!).
First, we need to enable macros in Outlook. Head to “File → Options” and enable macros. Note that this can pose a security risk for unauthorised code, so be careful when macros are installed by third parties.
Then open up the VBA editor through the keyboard combination of “ALT+F11”.
In the Project window navigate to “ThisOutlookSession” & insert some new code.
The VBA code I suggest that you use has been written by AdamThorne and others over the VBForums — check out their thread for more information. There is an additional fix to enable both in-line (i.e. reply in pane) and sending mail from a new window. This combined code is reproduced at the bottom of this article.
Save the code & then close the VBA coding interface. Now, every time you send an email out of hours you will be asked if you want to send the email now or at a later time.
This solution works well for Outlook in Windows. If anyone finds free solutions for other email clients, please drop me a line and I’ll update this post!
Dim obj As Object
Dim Mail As Outlook.MailItem
Dim WkDay As Integer
Dim MinNow As Integer
Dim SendHour As Integer
Dim SendDate As Date
Dim SendNow As String
Dim UserDeferOption As IntegerFunction getActiveMessage() As Outlook.MailItemDim insp As Outlook.InspectorIf TypeOf Application.ActiveWindow Is Outlook.Inspector Then
Set insp = Application.ActiveWindow
End IfIf insp Is Nothing Then
Dim inline As Object
Set inline = Application.ActiveExplorer.ActiveInlineResponse
If inline Is Nothing Then Exit FunctionSet getActiveMessage = inline
Else
Set insp = Application.ActiveInspector
If insp.CurrentItem.Class = olMail Then
Set getActiveMessage = insp.CurrentItem
Else
Exit Function
End IfEnd IfEnd FunctionPrivate Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
'On Error GoTo ErrorHandler
'This sub used to delay the sending of an email from send time to the next work day at 8am.'Set Variables
SendDate = Now()
SendHour = Hour(Now)
MinNow = Minute(Now)
WkDay = Weekday(Now)
SendNow = "Y"'Check if Before 7am
If SendHour < 7 Then
MsgBox ("Before seven")
SendHour = 8 - SendHour
SendDate = DateAdd("h", SendHour, SendDate)
SendDate = DateAdd("n", -MinNow, SendDate)
SendNow = "N"
End If'Check if after 7PM other than Friday
If SendHour >= 19 Then 'After 7 PM
SendHour = 32 - SendHour 'Send a 8 am next day
SendDate = DateAdd("h", SendHour, SendDate)
SendDate = DateAdd("n", -MinNow, SendDate)
SendNow = "N"
End If'Check if Sunday
If WkDay = 1 Then
SendDate = Now()
SendHour = Hour(Now)
SendDate = DateAdd("d", 1, SendDate)
SendDate = DateAdd("h", 8 - SendHour, SendDate)
SendDate = DateAdd("n", -MinNow, SendDate)
SendNow = "N"
End If'Check if Saturday
If WkDay = 7 Then
SendDate = Now()
SendHour = Hour(Now)
SendDate = DateAdd("d", 2, SendDate)
SendDate = DateAdd("h", 8 - SendHour, SendDate)
SendDate = DateAdd("n", -MinNow, SendDate)
SendNow = "N"
End If'Check if Friday after 7pm
If WkDay = 6 And SendHour >= 19 Then 'After 7pm Friday
SendDate = Now()
SendHour = Hour(Now)
SendDate = DateAdd("d", 3, SendDate)
SendDate = DateAdd("h", 8 - SendHour, SendDate)
SendDate = DateAdd("n", -MinNow, SendDate)
SendNow = "N"
End If'Send the Email
Set obj = getActiveMessage()
If obj Is Nothing Then
'Do nothing - as this is likely a calendar issue
'MsgBox "No active inspector"
ElseIf TypeOf obj Is Outlook.MailItem Then
Set Mail = obj
'Check if we need to delay delivery
If SendNow = "N" Then
UserDeferOption = MsgBox("Do you want to postpone sending until work hours (" & SendDate & ")?", vbYesNo + vbQuestion, "Time to stop working!")
If UserDeferOption = vbYes Then
Mail.DeferredDeliveryTime = SendDate
'MsgBox ("Your mail will be sent at: " & SendDate)
ElseEnd If
End IfEnd If
End IfExit Sub
'ErrorHandler:
' MsgBox "Error!"
End Sub
Delay sending code written by AdamThorne and others over the VBForums with an additional robustness to enable in-line editing from Javier Peletier at StackOverflow. [Code update 13/02/2017 — fixed for Calendar sending]
If you like this post, please hit the green heart and recommend it to others.
You can find more of our work over at: http://www2.expmicromech.com/publications
You can head over to twitter to follow Dr Ben Britton as @BMatB, or keep up to date with the group’s work via @ExpMicroMech.