How to write a filter for AutoFormatRule
-
Monday, June 13, 2011 11:08 AM
I am trying to add a filter to get all the subjects coloured Red which is sent to me(I am in the To list not in CC)
I tried many options but none of them worked to filter please help. I have the same code as below.
All the mails are colored red always it does not apply the filter.
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.Filter = "urn:schemas:mailheader:to LIKE emailkpaul@gmail.com";
//afr.Filter = "urn:schemas:httpmail:to LIKE emailkpaul@gmail.com";
//afr.Filter = "urn:schemas:httpmail:fromemail" + " = 'emailkpaul@gmail.com'";
//afr.Filter = "[Subject] = 'test'";
//afr.Filter ="\"urn:schemas:httpmail:fromemail\"" +" = 'emailkpaul@gmail.com'";
string criteria = "@SQL=\"urn:schemas:httpmail:subject\" = 'test'";
//string criteria = "@SQL=\"urn:schemas-microsoft-com:office:office#Subject\" = 'test'";
afr.Filter = criteria;
MessageBox.Show(afr.Filter);
afr.Font.Color = OlColor.olColorRed;
afr.Enabled = true;
tv.Save();
tv.Apply();
MessageBox.Show("Done");
}
catch(System.Exception ex)
{
MessageBox.Show("Err:" + ex.Message);
}
}
All Replies
-
Monday, June 13, 2011 3:56 PM
Creating that rule manually from the Outlook UI returns the following for the Filter property:
"http://schemas.microsoft.com/mapi/proptag/0x0e04001f" CI_STARTSWITH 'user@domain.com'
Dmitry Streblechenko (MVP)
http://www.dimastr.com/redemption
Redemption - what the Outlook
Object Model should have been
-
Tuesday, June 14, 2011 11:03 AM
I created a filter from Outlook UI. I got the Filter string in code for that.
Then I tried creating a new AutoFormatRule with that same filter string. but even then it doesnot get applied to the new rule that i created. I dont know why is that?
-
Tuesday, June 14, 2011 4:13 PMDo you see the new rule with the correct filter in the Outlook UI?
Dmitry Streblechenko (MVP)
http://www.dimastr.com/redemption
Redemption - what the Outlook
Object Model should have been -
Wednesday, June 15, 2011 5:26 AM
No, the new rule(created from C#) is displayed in list of rules in Outlook UI. It shows the font settings correctly. but Filter is not set at all. If I go and set it(filter) in Outlook UI it works. But when I try to set the same from C# it doesn't set the filter. I don't understand this behavior. I feel its a bug with Outlook. Is it so?
I am struglling because of this issue and is not able to proceed further.
-
Friday, June 17, 2011 6:29 AM
The following script (you can run it from OutlookSpy or Outlook VBA) worked perfectly for me:
set tv = application.ActiveExplorer.CurrentView
set afr = tv.AutoFormatRules.Add("TestAFRule")
afr.Filter = " ""http://schemas.microsoft.com/mapi/proptag/0x0e04001f"" CI_STARTSWITH 'test@dimastr.com' "
afr.Font.Color = olColorRed
afr.Enabled = true
tv.Save
tv.Apply
Dmitry Streblechenko (MVP)
http://www.dimastr.com/redemption
Redemption - what the Outlook
Object Model should have been
- Proposed As Answer by Calvin_GaoModerator Monday, June 27, 2011 3:57 AM
- Marked As Answer by Calvin_GaoModerator Wednesday, June 29, 2011 2:10 AM
- Unmarked As Answer by Calvin_GaoModerator Wednesday, July 27, 2011 3:09 AM
-
Monday, June 27, 2011 3:57 AMModerator
Hi KavithaPaul,
Any update? Have you tried the code provided by Dmitry? Does this helps?
Best Regards, Calvin Gao [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

-
Tuesday, July 26, 2011 11:01 AMI tried it but not working
-
Wednesday, July 27, 2011 2:07 AMWhen and how did you try it?
Dmitry Streblechenko (MVP)
http://www.dimastr.com/redemption
Redemption - what the Outlook
Object Model should have been -
Wednesday, October 31, 2012 9:48 PMI am having the same problem trying to create an AutoFormatRule programmatically from a .NET Outlook Add-in. The rule is created and the font is set correctly but my filter is not being set. Has anyone resolved this issue?
-
Wednesday, October 31, 2012 10:16 PMHave you tried the script above?
Dmitry Streblechenko (MVP) http://www.dimastr.com/redemption
Redemption - what the Outlook
Object Model should have been
Version 5.4 is now available!
-
Tuesday, February 26, 2013 8:02 AM
hi Dmitry,
maybe I'm a bit late to this Topic, but I have a similar Problem. I tried your code and I have the problem the guys described before. The rule is created and the fontsettings are perfect. But the filter in the UI is empty.
Anyway the rules I create by code are working. I'm setting the filter with the following code. If I do that on Outlook 2007 SP2 the tableview shows "wird geladen" (on a english UI it must be something like "is loading"). But never stops showing that. On a earlier Outlook 2k7 or Outlook 2010 and later everthing works fine. You have any idea how I can reset my filter so Outlook 2k7 SP2 will stop loading?
thx. Jürgen
internal const string c_RuleFilterSchema = "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/"; internal const string c_VariableState = "BRImportState"; string state = "Completed"; string filter = "\"" + Connect.c_RuleFilterSchema + Connect.c_VariableState + "/0000001f\" = '" + state + "'"; rule.Filter = filter;
-
Wednesday, February 27, 2013 6:02 AMI don't know, sorry.
Dmitry Streblechenko (MVP) http://www.dimastr.com/redemption
Redemption - what the Outlook
Object Model should have been
Version 5.4 is now available!

