1

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?

Vidar
  • 11

1 Answers1

0

A possible alternative if the original code gives odd results.

Option Explicit

Public Sub Check_ReceivedTime_SenderEmailAddress(newMail As MailItem)

Dim objMail As MailItem
Dim ReceivedHour As Integer
Dim msg As String

ReceivedHour = Hour(newMail.ReceivedTime)

If ReceivedHour >= 22 Or ReceivedHour < 8 Then

    Set objMail = CreateItem(olMailItem)
    msg = "This is the email body text..."

    With objMail

        ' The sender may have set up a replyTo address
        '  which is now ignored
        ' https://www.lifewire.com/replies-go-to-different-address-outlook-1173678

        .To = newMail.SenderEmailAddress
        .Body = msg
        .Display
        ' or
        ' .Send

    End With

Else

    Debug.Print "During office hours. Do not sent the automated reply."

End If

End Sub
niton
  • 1,832