How to create a custom rule using Visual Basic for Applications (VBA) in Outlook 2002

Article translations Article translations
Article ID: 292063 - View products that this article applies to.
This article was previously published under Q292063
For a Microsoft Outlook 2000 version of this article, see 235852.
Expand all | Collapse all

SUMMARY

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.

MORE INFORMATION

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.
  1. On the Tools menu, point to Macro, and click Visual Basic Editor.
  2. In the Project - Project1 pane, double-click Project1, and double-click Microsoft Outlook Objects.
  3. Double-click ThisOutlookSession to open a code window.
  4. 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.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 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 "myaddress@mydomain.com"
    
             ' Send it
             myForward.Send
    
          End If
    
       End If
    
    End Sub
    					
  5. On the File menu, click Save VBAProject.otm.
  6. Close the Visual Basic Editor.
  7. Restart Outlook so that the code will run.
Note For 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.

REFERENCES

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

Properties

Article ID: 292063 - Last Review: March 3, 2006 - Revision: 4.2
APPLIES TO
  • Microsoft Outlook 2002 Standard Edition
Keywords: 
kbprogramming kbvba kbhowto KB292063

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com