Export (0) Print
Expand All

How to: Create a Rule to File Mail Items from a Manager and Flag Them for Follow-Up

This example shows how to set up a rule to file mail items from the user’s manager and flag them for follow up.

Programming Applications for Office Outlook 2007

The following code example is an excerpt from Programming Applications for Microsoft Office Outlook 2007, from Microsoft Press (ISBN 9780735622494, copyright Microsoft Press 2007, all rights reserved).

Buy this book

Sample chapters

Outlook rules can operate either server-side or client-side, depending on the type of account and rule. There are many ways you can implement rules to enforce your own organizational schemes when you organize items in your mailbox. For example, you can create a subfolder hierarchy that organizes unread mail and read mail by subject area. Or, you can create a subfolder hierarchy that corresponds to the sender of the message. You can also categorize your mail and then use search folders to aggregate the mail by category.

The Rules object model, which includes a Rule object that represents a rule in Outlook, allows you to create rules programmatically to enforce a certain organizational scheme, create a specific rule that is unique to your solution, or ensure that certain rules are deployed to a group of users. By using the Rules object model, you can programmatically add, edit, and delete rules. By using the Rules collection and the Rule object, you can also access, add, and delete rules defined for a session. A Rule object has a RuleType property that indicates whether the rule is a send or receive rule. When a rule is created, the RuleType property is specified, and cannot be changed without deleting and re-creating the rule with a different RuleType property. The RuleAction and RuleCondition objects, their collection objects, and derived action and condition objects are also used to further support editing rule actions and rule conditions.

The Rules object model does not support all rules that you can create by using the Rules and Alert Wizard in the Outlook user interface, but it supports the most commonly used rule actions and conditions. Any rules created by using the Rules and Alerts Wizard that are applied to messages, which include mail items, meeting requests, task requests, documents, delivery receipts, read receipts, voting responses, and out-of-office notices, can also be created programmatically.

A rule can execute on the Exchange server or on the Outlook client, provided that the current user’s mailbox is hosted on an Exchange server. The IsLocalRule property of the Rule object returns true to indicate that the rule executes on a client, and Outlook must be running for the rule to execute. If the rule executes on the server, Outlook does not have to be running for the rule conditions to be evaluated and the rule actions to be completed.

Note Note

There is no separate collection that represents rule exception conditions. Use the Exceptions property of the Rule object to get a RuleConditions collection that represents rule exception conditions.

To create rules through the Outlook object model, follow these steps:

  1. Get the Rules collection from the DefaultStore property of the NameSpace object by calling the GetRules() method on the default Store object. Use a try…catch block to account for the user being offline or disconnected from the Exchange server. This prevents Outlook from raising an error.

  2. Call the Create(String, OlRuleType) method on the Rules object to create an instance variable or a Rule object, specifying a Name and a OlRuleType parameter.

  3. Use the RuleActions and RuleConditions collections to enable actions, conditions, and exceptions on the Rule object. Note that any condition enabled in the RuleConditions collection, returned by the Exceptions property, is treated as a rule exception condition, and additional built-in custom actions or conditions cannot be added to the collection.

  4. Set the Enabled property to true for any given rule action, condition, or exception to be operational. Some actions or conditions, such as the Folder property, require that you set additional properties on the action or condition to save the Rule object without an error.

  5. Finally, call the Save(Object) method on the Rules collection to save the created or modified rules. Enclose the Save method in a try…catch block to handle exceptions.

In the following code example, CreateManagerRule implements the steps previously described. CreateManagerRule first verifies whether the CurrentUser property represents an ExchangeUser object, indicating that the current user is an Exchange user. If the current user is an Exchange user, CreateManagerRule gets the current user’s manager by calling the GetExchangeUserManager() method on the ExchangeUser object of the CurrentUser property of the NameSpace object. A receive rule is then created to move received messages to a subfolder of the Inbox for the following conditions:

  • The message is from the user’s manager.

  • The recipient is on the To: line of the message.

  • The message is not a meeting request or update.

Finally, the message is flagged for follow-up today. CreateManagerRule also illustrates appropriate error handling for conditions that could raise an exception such as the user being offline or disconnected in cached Exchange mode.

If you use Microsoft Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 14.0 Object Library component and specify the Outlook variable when you import the Microsoft.Office.Interop.Outlook namespace. The using statement must not occur directly before the functions in the code example but must be added before the public Class declaration. The following line of code shows how to do the import and assignment in C#.

using Outlook = Microsoft.Office.Interop.Outlook;
private void CreateManagerRule()
{
    Outlook.ExchangeUser manager;
    Outlook.Folder managerFolder;
    Outlook.AddressEntry currentUser =
        Application.Session.CurrentUser.AddressEntry;
    if (currentUser.Type == "EX")
    {
        try
        {
            manager = currentUser.
                GetExchangeUser().GetExchangeUserManager();
        }
        catch
        {
            Debug.WriteLine("Could not obtain user's manager.");
            return;
        }
        Outlook.Rules rules;
        try
        {
            rules = Application.Session.DefaultStore.GetRules();
        }
        catch
        {
            Debug.WriteLine("Could not obtain rules collection.");
            return;
        }
        if (manager != null)
        {
            string displayName = manager.Name;
            Outlook.Folders folders =
                Application.Session.GetDefaultFolder(
                Outlook.OlDefaultFolders.olFolderInbox).Folders;
            try
            {
                managerFolder =
                    folders[displayName] as Outlook.Folder;
            }
            catch
            {
                managerFolder =
                    folders.Add(displayName, Type.Missing)
                    as Outlook.Folder;
            }
            Outlook.Rule rule = rules.Create(displayName,
                Outlook.OlRuleType.olRuleReceive);

            // Rule conditions
            // From condition
            rule.Conditions.From.Recipients.Add(
                manager.PrimarySmtpAddress);
            rule.Conditions.From.Recipients.ResolveAll();
            rule.Conditions.From.Enabled = true;

            // Sent only to me
            rule.Conditions.ToMe.Enabled = true;

            // Rule exceptions
            // Meeting invite or update
            rule.Exceptions.MeetingInviteOrUpdate.Enabled = true;

            // Rule actions
            // MarkAsTask action
            rule.Actions.MarkAsTask.MarkInterval =
                Outlook.OlMarkInterval.olMarkToday;
            rule.Actions.MarkAsTask.FlagTo = "Follow-up";
            rule.Actions.MarkAsTask.Enabled = true;

            // MoveToFolder action
            rule.Actions.MoveToFolder.Folder = managerFolder;
            rule.Actions.MoveToFolder.Enabled = true;
            try
            {
                rules.Save(true);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
    }
}

Other Resources

Community Additions

ADD
Show:
© 2014 Microsoft