OL2000: How to Create a Custom Rule Using Visual Basic for Applications

ID: Q235852


The information in this article applies to:
  • Microsoft Outlook 2000


SUMMARY

If the 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, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals 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 needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp
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:


  5. 
    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 
  6. On the File menu, click Save VBAProject.otm.


  7. Close the Visual Basic Editor.


  8. Restart Outlook so that the code will run.


NOTE: For more information about how to make sure your code properly handles various types of items, please see the following article in the Microsoft Knowledge Base:
Q222482 OL2000: 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 additional information about available resources and answers to commonly asked questions about Microsoft Outlook 2000 solutions, please see the following article in the Microsoft Knowledge Base:

Q146636 OL2000: Questions About Custom Forms and Outlook Solutions

Additional query words: OutSol OutSol2000 vbscript

Keywords : kbdta
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: November 13, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.