Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Forward specific e-mails at certain times in Outlook using VBA

Status
Not open for further replies.

luxcs

Technical User
Mar 23, 2004
8
GB
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:

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
 
Send yourself an email with the following code in place.

Code:
Private Sub Application_NewMail()
   MsgBox "You've got mail!"
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top