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
  • 3
  • What is the exact issue with your code? Doesn't work? Do you get any error message? Did you tried debugging it? ... – Máté Juhász Sep 18 '17 at 11:20
  • The original code sends an auto reply for emails received before 15:00. But my altered code doesn't send any auto reply. The only part changed is the IF statement (If ReceivedHour >= 22 OR ReceivedHour < 8 Then), so there has to be something wrong here. I suspect that it has something to do with 12/24 hour time format, but I don't know how to fix this... So no, I don't get any error message, the auto reply simply doesn't work. I haven't tried any debugging, I don't know how to do this. – Vidar Sep 18 '17 at 11:39
  • Does anyone know how to format the ReceivedHour times so the script will work between 22:00 and 08:00? – Vidar Sep 19 '17 at 07:12
  • https://stackoverflow.com/documentation/vba/802/getting-started-with-vba/15512/debugging#t=201709190738025803519 – Máté Juhász Sep 19 '17 at 07:39
  • I have researched more and discovered the script actually works. It sends the auto reply between 2200 and 0800, but only for e-mails where the sender hasn't defined their display name. So for an e-mail from just "donald@duck.com" it will send the auto reply, but for an e-mail from "Donald Duck " or equivalent it won't send an auto reply. In addition, the auto reply seems to be sent from the original sender's address, and not my own. It looks kind of strange when Donald Duck receives an auto reply from himself... Any ideas on how to fix these two matters? – Vidar Oct 05 '17 at 11:46
  • So it looks like https://www.neowin.net/forum/topic/1235529-automatic-out-of-office-after-1500-mon-fri/ will solve your issue – Selkie Dec 21 '17 at 22:10

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,796
  • 14
  • 21