Rule or VBA Code to auto forward an email from Specific person to another email id received during a specific time


I am looking for a Rule or VBA code which would enable Auto forward all the emails received during a specific frame (05:01AM CST to 04:29 AM CST). The email received from that specific email id must be forwarded to another email with the message eg: "we are unable to process the order as we have received the order outside our coverage cycle".

I am new to vba coding so it would be helpful if you provide steps to activate the code.

Thank You.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
To be clear:
* Only mails sent from a specific mail ID should be forwarded.
* It should be forwarded only within a particular timeframe (but certainly not 05:01 am to 04:29, as that would leave only half an hour without forwarding the mail).
* The target mail ID is a fixed one.

If all that is true, you can use either a VBA-only approach, or mix rule and VBA. The VBA part is required as you need to check for a timeframe, and that cannot be done with rules.
This is the vBA-only code. You might have to change the folder checked for new mails (which is the Inbox used below), and of course the times and mail IDs.
Option Explicit

Public WithEvents myOlItems As Outlook.Items

Public Sub Application_Startup()
  Set myOlItems = Session.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub myOlItems_ItemAdd(ByVal Item as Object)
Dim ml As MailItem
  if TypeName(Item) <> "MailItem" Then Exit Sub
  If Item.SenderEmailAddress = "" Then
    If SentOn < Date+TimeValue("04:30") or SentOn > Date+TimeValue("05:00") Then
      Set ml = Item.Forward
      ml.Recipients.Add ""
      ml.Body = "We are unable to process the order as we have received the order outside our coverage cycle" _
                + vbCrLf + vbCrLF + ml.Body
    End If
  End If
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chethannAuthor Commented:
I tried with the above solution but it doesn't seem to work. I changed the email address and the timings but it does not work.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
You need to restart Outlook, or at least run the Application_Startup macro, to activate the event trigger. Then drop a mail into the inbox to see if it does something. For testing, it is always a good start to set a breakpoint (F9) on a line inside the called sub, e.g. in line 14 above, and then single-step thru the code, viewing variables if required.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.