I'm trying to use VBA to forward all certain e-mails, received from specific e-mail addresses only, when received outside of office hours.
ie. On a Saturday, Sunaday, or outside of 8am-6pm Monday to Friday, I'd like e-mails from email1@domain1 and email2@domain2to be forwarded to an alternate e-mail address.
I've used VB and VBA before, just never with Outlook, and am looking for some assistance/guidance. I've searched the Net already, but can't get my code to work.
Also, I already have quite a lot of rules runnings to filter e-mails into specific folders. So, would this run before the rules pickup the e-mail, or can I check all folders for new mail from these addresses?
Here's what I currently have:
Thanks for any help given
Chris
ie. On a Saturday, Sunaday, or outside of 8am-6pm Monday to Friday, I'd like e-mails from email1@domain1 and email2@domain2to be forwarded to an alternate e-mail address.
I've used VB and VBA before, just never with Outlook, and am looking for some assistance/guidance. I've searched the Net already, but can't get my code to work.
Also, I already have quite a lot of rules runnings to filter e-mails into specific folders. So, would this run before the rules pickup the e-mail, or can I check all folders for new mail from these addresses?
Here's what I currently have:
Code:
Public WithEvents myOlItems As Outlook.Items
Public Sub Application_Startup()
' Reference the items in the Inbox. Because myOlItems is declared
' "WithEvents" the ItemAdd event will fire below.
Set myOlItems = Outlook.Session.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub myOlItems_ItemAdd(ByVal Item As Object)
' If it's currently not between 8:00 A.M. and 6:00 P.M.
If Time() < #8:00:00 AM# Or Time() > #6:00:00 PM# Then
' Check to make sure it is an Outlook mail message, otherwise
' subsequent code will probably fail depending on what type
' of item it is.
If TypeName(Item) = "MailItem" Then
' Forward the item just received
Set myForward = Item.Forward
' Address the message
myForward.Recipients.Add "alternate@domain"
' Send it
myForward.Send
End If
End If
End Sub
Thanks for any help given
Chris