Programmatically creating auto-format rule in Outlook

Here is the code snippet…

private void AutoFormat()
{
      try
      {
            AutoFormatRule afr;
            TableView tv;
            tv = applicationObject.ActiveExplorer().CurrentView;
            MessageBox.Show(tv.AutoFormatRules.Count.ToString());
            for (int i=1;i<=tv.AutoFormatRules.Count;i++)
            {
                  AutoFormatRule rl =  tv.AutoFormatRules[i];
                  if(rl.Name=="TestAFRule")
                  {
                        tv.AutoFormatRules.Remove(i);
                        MessageBox.Show("Deleted" + rl.Name);
                  }
            }
            afr = tv.AutoFormatRules.Add("TestAFRule");               
            afr.Font.Color = OlColor.olColorRed;                
            afr.Enabled = true;
            tv.Save();
            tv.Apply();
            MessageBox.Show("Done");
      }
      catch(System.Exception ex)
      {
            MessageBox.Show("Err:" + ex.Message);
      }
}

 

Advertisements
This entry was posted in Outlook, VSTO and tagged , , , by Pranav. Bookmark the permalink.

About Pranav

A Software Engineer by profession and a Geek at heart. I love exploring new technologies and working on algorithmic problems. As of now, I write code for bread, butter and satisfaction. I like to develop applications, and generally that involves much more than writing code. It’s not just about hurling code, but producing something meaningful that has a measurable and positive impact, helps others and solves interesting problems.

One thought on “Programmatically creating auto-format rule in Outlook

  1. Could you help me
    I need to create a rule in outlook for defer a delivery message
    The code is like that :
    Set colRules = Application.Session.DefaultStore.GetRules()
    ‘Create the rule by adding a SendRule to Rules collection
    Set oRule = colRules.Create(“Envoi différé”, olRuleSend)
    Set oDeferRuleAction = oRule.Actions(OlRuleActionType.olRuleActionDefer)
    With oDeferRuleAction
    .enabled = True
    then the first action is activate “delete category” but not defer delivery and I can’t set a number of minutes

    End With
    some thing is missing I don’t know

    tank’s a lot for your help

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s