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

Getting Rules from an exchange account in Outlook

Getting Rules from an exchange account configured in Outlook is pretty easy though many developers who is working on it for the first time may confuse with the approach. I banged my head for 2 days to get the exact function and attributes to get them.

Dim olRules As Outlook.Rules = Globals.ThisAddIn.Application.Session.DefaultStore.GetRules

For Each olRule As Outlook.Rule In olRules
If olRule.Conditions.From.Recipients.Count > 0 Then
‘ CONDITION: FROM EMAIL
Dim exUser As Outlook.ExchangeUser = olRule.Conditions.From.Recipients.Item(1).AddressEntry.GetExchangeUser
Dim FromEmail As String = exUser.PrimarySmtpAddress
MessageBox.Show(“FROM:  ” & FromEmail)
ElseIf olRule.Conditions.SentTo.Enabled Then
‘ CONDITION: TO EMAIL
Dim exUser As Outlook.ExchangeUser = olRule.Conditions.To.Recipients.Item(1).AddressEntry.GetExchangeUser
Dim ToEmail As String = exUser.PrimarySmtpAddress
MessageBox.Show(“TO: ” & ToEmail)
ElseIf olRule.Conditions.Subject.Enabled Then
‘ CONDITION: SUBJECT
MessageBox.Show(“Subject: ” & olRule.Conditions.Subject.Text)
End If
‘ ACTION: MOVE TO FOLDER
If olRule.Actions.Item(1).ActionType = Outlook.OlRuleActionType.olRuleActionMoveToFolder Then
MessageBox.Show(“Move To Folder: ” & olRule.Actions.MoveToFolder.Folder.FolderPath)
End If
Next
Hope this Helps!