I am using Microsoft Outlook 2016 Home. I would like to set up an "Out of Office" auto reply for all incoming emails between 22:00 and 08:00 local time.
I have understood that I need to apply a script to accomplish this. I found this one script that sends auto reply during day time, for emails received before 15:00. This script works like a charm, so I tried to alter it to work at night time, for emails received between 22:00 and 08:00.
The original script is here: How do I set up Outlook to send a auto reply during a certain hour of the day every day?
My altered script is here:
Public Sub Check_ReceivedTime(newMail As Outlook.MailItem)
Dim obj As Object
Dim ReceivedHour As Integer
Dim newReply As MailItem
Dim msg As String
ReceivedHour = Hour(newMail.ReceivedTime)
If ReceivedHour >= 22 OR ReceivedHour < 8 Then
Set newReply = newMail.reply
msg = "This is the email body text..."
CreateMail newReply.To, msg
Else
Debug.Print "During office hours. Do not sent the automated reply."
End If
Set newReply = Nothing
End Sub
Private Sub CreateMail(ReplyAddress As String, msg As String)
Dim objMail As Outlook.MailItem
Set objMail = CreateItem(olMailItem)
With objMail
.To = ReplyAddress
.Body = msg
.Display
' or
' .Send
End With
End Sub
I suspect that there may be an issue with 12/24 hour format perhaps, but I'm not able to fix this myself. Can anyone help me, please?