Forward all incoming emails

Hello,
Can you please help,
I need to automatically forward all incoming emails (to a specific account (1 or more) between 7:pm to 5:00 am to (one or more) email addresses.
is there a vba that I can put in the This Outlook session , that can do this.

1. Start Outlook
2. Click Tools->Macro->Visual Basic Editor
3. If not already expanded, expand Microsoft Outlook Objects in the Project pane, and then click on "ThisOutlookSession"
4. Copy the script below
5. Paste the script into the right-hand pane of the VB Editor
6. Edit the script making the changes as per the comments I included in the code
7. Click the diskette icon on the toolbar to save the changes
8. Close the VB Editor
9. Click Tools->Macro->Security
10. Set Security Level to Medium
11. Close Outlook
12. Start Outlook
13. A dialog-box will appear telling you the ThisOutlookSession contain macros and asking if you want to enable them. Say yes.
14. Test the macro
15. When the macro runs Outlook will present you with another dialog-box advising that a program is trying to access your mailbox and asking if you want to allow it to. Say yes.
16. If a message is received in the mailbox, and if the time is currently between the times you set, then the message will be forwarded to the designated email address. Otherwise, nothing will happen.
17. Once you've verified that the macro works as expected, then you need to sign the macro to avoid having Outlook security warn you each time the macro runs.

'Put your email address to forward
objMail.To = abc@gmail.com"
objMail.Send
Set objItem = Nothing
Set objMail = Nothing
End If
End If
Next
End Sub
Function IsNothing(Obj)
If TypeName(Obj) = "Nothing" Then
IsNothing = True
Else
IsNothing = False
End If
End Function

I'd do it a bit differently. I'd create a rule that fires for all new messages and set that rule to run the following macro.

Sub AutoForwardMessages(Item As Outlook.MailItem) 'On the next line, edit the time forwarding is to begin Const TIME_BEG = #7:00:00 PM# 'On the next line, edit the time that forwarding is to end Const TIME_END = #5:00:00 AM# 'On the next line, edit the address that messages are to be forwarded to Const ACCT_ADDR = "someone@gmail.com" Dim olkFwd As Outlook.MailItem If (Time >= TIME_BEG) And (Time <= TIME_END) Then Set olkFwd = Item.Forward olkFwd.To = ACCT_ADDR olkFwd.Send End If Set olkFwd = NothingEnd Sub

As a test or as a permanent solution? If it's the former, then yes. Add this code to what you already have, then select a message and run TestAFM. If it's the latter, then that's possible too, but will require more code. The rule is a simpler and more flexible solution.

Ok. Use this version instead. This code must go in the ThisOutlookSession module.

Dim WithEvents olkApp As Outlook.ApplicationPrivate Sub Application_Startup() Set olkApp = ApplicationEnd SubPrivate Sub Application_Quit() Set olkApp = NothingEnd SubPrivate Sub olkApp_NewMailEx(ByVal EntryIDCollection As String) Dim arrEID As Variant, varEID As Variant, olkItm As Object arrEID = Split(EntryIDCollection, ",") For Each varEID In arrEID Set olkItm = Session.GetItemFromID(varEID) If olkItm.Class = olMail Then AutoForwardMessages olkItm End If Next Set olkItm = NothingEnd SubSub AutoForwardMessages(Item As Outlook.MailItem) 'On the next line, edit the time forwarding is to begin Const TIME_BEG = #7:00:00 PM# 'On the next line, edit the time that forwarding is to end Const TIME_END = #5:00:00 AM# 'On the next line, edit the address that messages are to be forwarded to Const ACCT_ADDR = "someone@gmail.com" Dim olkFwd As Outlook.MailItem If (Time >= TIME_BEG) And (Time <= TIME_END) Then Set olkFwd = Item.Forward olkFwd.To = ACCT_ADDR olkFwd.Send End If Set olkFwd = NothingEnd Sub

Replace the code you have now with the version below. Close and restart Outlook. Let me know what happens when a message arrives.

Dim WithEvents olkApp As Outlook.ApplicationPrivate Sub Application_Startup() Set olkApp = ApplicationEnd SubPrivate Sub Application_Quit() Set olkApp = NothingEnd SubPrivate Sub olkApp_NewMailEx(ByVal EntryIDCollection As String) Dim arrEID As Variant, varEID As Variant, olkItm As Object arrEID = Split(EntryIDCollection, ",") MsgBox "NewMailEx fired" For Each varEID In arrEID Set olkItm = Session.GetItemFromID(varEID) If olkItm.Class = olMail Then AutoForwardMessages olkItm End If Next Set olkItm = NothingEnd SubSub AutoForwardMessages(Item As Outlook.MailItem) 'On the next line, edit the time forwarding is to begin Const TIME_BEG = #7:00:00 PM# 'On the next line, edit the time that forwarding is to end Const TIME_END = #5:00:00 AM# 'On the next line, edit the address that messages are to be forwarded to Const ACCT_ADDR = "someone@gmail.com" Dim olkFwd As Outlook.MailItem MsgBox "AutoForwardMessages fired" If (Time >= TIME_BEG) And (Time <= TIME_END) Then Set olkFwd = Item.Forward olkFwd.To = ACCT_ADDR olkFwd.Send End If Set olkFwd = NothingEnd Sub

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

2. Adjust the time in the code as needed.
3. Select a message in your inbox.
4. Switch back to the code
5. Place the cursor inside the TestAFM subroutine
6. Press F8. This will start executing the code and turn the first line of code in TestAFM yellow. Each time you press F8 Outlook will execute the highlighted line of code and move to the next line. What I want you to do is step through the code one line at a time and see what happens.

here's my finding,
on any new email, the forward code is not triggered automatically as I receive them.
However,
if I highlight any email in my inbox, and run the TestAFM Code, I do get the "Fired" message, and I do receive the forwarded email.

Ok. So the code works but is not firing automatically like it should. Is there any other code in ThisOutlookSession? Do you have any rules in place that are moving messages as they arrive? Can you post a screenshot of the code as it appears in the VB editor in Outlook?

How about rules? Any rules that are moving messages as they arrive? If not, then the only explanation I can think of is that Outlook is not executing the Startup procedure as it should. To test that theory, please replace the subroutine Application_Startup with the version below. Leave the rest of the code as is. Once you've done that, please close and restart Outlook. As Outlook starts it should display a pop-up saying "Application_Stratup ran". Please let me know if that happens. If it does, then please test by sending yourself a message.

I found the problem when I switched to using the time-frame you have set on your computer. Here's what's happening. It's 2:47PM my time and the time-frame set in the code is 1:00PM to 7:00AM. The is the line of code that tests to see if the current time is in the desired time-frame

Let's think about that in English Is 2:47PM >= 1:00PM? Yes it is. Good so far. Now, is 2:47PM <= 7:00AM? No, it's not. That causes the code to fall through without forwarding the message. It's a result of the time-frame crossing days. if the time-frame was all on the same day, then everything would work properly.

Here's a new version of the code that should fix the problem. Please delete all the code you have now and replace it with this version. I've included some debugging code that outputs a log file we can use in case there are still problems.

Featured Post

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels.
Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…

The viewer will learn how to create a slide that will launch other presentations in Microsoft PowerPoint.
In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide:
…