This article was previously published under Q292063
For a Microsoft Outlook 2000 version of this article, see 235852.
If the Microsoft Outlook Rules Wizard does not provide a feature that meets your mail-routing needs, you can perhaps use Outlook Visual Basic for Applications to create a custom rule. This article describes how to get started setting up a simple rule and discusses some important considerations to keep in mind when creating a rule using Visual Basic for Applications.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
The following steps create a rule that automatically forwards any mail you receive outside of regular business hours to another e-mail address, such as the e-mail address you use at home, or to someone else who works the shift after yours.
On the Tools menu, point to Macro, and click Visual Basic Editor.
In the Project - Project1 pane, double-click Project1, and double-click Microsoft Outlook Objects.
Double-click ThisOutlookSession to open a code window.
In the code window, type the following code. Modify the line that specifies the e-mail address; substitute the e-mail address you want e-mail forwarded to:
Public WithEvents myOlItems As Outlook.ItemsPublic 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).ItemsEnd SubPrivate Sub myOlItems_ItemAdd(ByVal Item As Object) ' If it's currently not between 9:00 A.M. and 5:00 P.M. If Time() < #9:00:00 AM# Or Time() > #5: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 "firstname.lastname@example.org" ' Send it myForward.Send End If End IfEnd Sub
On the File menu, click Save VBAProject.otm.
Close the Visual Basic Editor.
Restart Outlook so that the code will run.
NoteFor more information about how to make sure that your code properly handles various types of items, click the following article number to view the article in the Microsoft Knowledge Base:
292062 How to handle unexpected items in a collection
The following considerations should be taken into account if you are considering implementing a rules solution using Visual Basic for Applications:
Outlook must be running for the Visual Basic for Application code to run. This is the same effect as using client-side rules in the Rules Wizard. If Outlook is not running at the time the message arrives, the rule will not work.
Your custom rule may conflict with other rules you have set up using the Rules Wizard. For example, if you are using Microsoft Exchange Server and create a server-side rule that moves mail from a specific individual to a specific folder, the mail is moved on the server and never reaches the Inbox. Therefore, the ItemAdd event does not run since an item is not added to the Inbox.
The code runs regardless of how an item is added to the Inbox. For example, if you work late one night and drag a message to your Inbox, the message is forwarded to the other e-mail account.
Outlook also has a NewMail event, but that event only runs when you get a new mail notification. If you receive three messages at once, the event only runs once. You can use the NewMail event to make sure that your Visual Basic for Applications code only runs when you receive new messages, but you must add additional logic to the code to search for those messages that haven't been read before. So unfortunately both events tend to have an unwanted side effect:
The ItemAdd event makes it easy to act on all of the incoming items, but it also acts on those items you move to the Inbox yourself.
The NewMail event does not run when you drag items to the Inbox, but it is more difficult to program a solution to take into account that there are multiple items in the Inbox that your code must act on.
For more information about available resources, and for answers to commonly asked questions about Microsoft Outlook solutions, click the following article number to view the article in the Microsoft Knowledge Base:
287530 Frequently asked questions about custom forms and Outlook solutions