Outlook VSTO 加载项:未应用 AutoFormatRule 筛选器

Outlook VSTO Add In: AutoFormatRule Filter is not applied

我正在尝试以编程方式将自动格式规则应用于 Outlook 2016。首先,我手动创建了一个规则并读取过滤器 属性,我有这样的过滤器:

"(\"urn:schemas:httpmail:read\" = 0 AND \"http://schemas.microsoft.com/mapi/proptag/0x001a001e\" = 'IPM.Note.MyMessage')"

然后我尝试以编程方式应用它:

Dictionary<string, OlColor> colorizationRules = new Dictionary<string, OlColor>()
        {
            {Resources.MsgClass1, OlColor.olColorRed},
            {Resources.MsgClass2, OlColor.olColorYellow},
            {Resources.MsgClass3, OlColor.olColorGreen}
        };

        Explorer explorer = Application.ActiveExplorer();

        if (explorer != null)
        {
            TableView tableView = explorer.CurrentView as TableView;

            if (tableView != null)
            {
                IEnumerable<AutoFormatRule> rules = tableView.AutoFormatRules.Cast<AutoFormatRule>();

                foreach (KeyValuePair<string, OlColor> coloriztionRule in colorizationRules)
                {                       
                    AutoFormatRule newRule = tableView.AutoFormatRules.Add(coloriztionRule.Key);

                    newRule.Filter = $"(\"urn:schemas:httpmail:read\"=0 AND \"http://schemas.microsoft.com/mapi/proptag/0x001a001e\"='{coloriztionRule.Key}')";
                    newRule.Font.Color = coloriztionRule.Value;
                    newRule.Enabled = true;

                    tableView.AutoFormatRules.Save();
                    tableView.Save();
                    tableView.Apply();
                }
            }
        }

已创建规则,但未应用过滤器值。

其中一个建议是过滤器值必须以“@SQL=...”为前缀。但那是行不通的。

然后我找到了这个话题Outlook 2010 AutoFormatRule.Filter property not saving

响应是:

I raised a premier call in response to this issue. The response was that it is a known bug in the Outlook Object Model. It will not be fixed in Outlook 2010 or Outlook 2013 as the risk is too great for the small change.

建议的解决方法是:

The workaround solution that Microsoft provided was to copy a rule from a Public Folder to the user's profile.

这是什么意思? 是否有任何其他解决方法可以使规则在 C# 代码中起作用?

这是 Outlook 中的一个错误,MS 不打算修复它。

这个问题是有解决办法的,虽然有点乱。这绝对是一个 Outlook 错误,导致以编程方式创建的 AutoFormatRules(通过 Outlook UI 查看条件格式)在半损坏状态下创建,不会持续更改视图或重新加载应用程序。您可以通过向 Explorer.ViewSwitch 事件添加事件处理程序来解决此问题,该事件会在每次更改视图时删除并重新创建 AutoFormatRule。这将使您的条件格式规则看起来好像工作正常。

我们已使用此技术实现条件格式设置,通过具有基于邮件项目的 crmLinkState 自定义属性值的条件格式设置规则,突出显示待跟踪到 Dynamics CRM 的电子邮件。

    public partial class BrethertonsAddIn
    {
        Outlook.Explorer _activeExplorer;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            _activeExplorer = this.Application.ActiveExplorer();
            _activeExplorer.ViewSwitch += BrethertonsAddIn_ViewSwitch;
            BrethertonsAddIn_ViewSwitch();
        }

        private void BrethertonsAddIn_ViewSwitch()
        {
            Outlook.Explorer olExplorer = Application.ActiveExplorer();
            // Convert the Outlook view into a COM _TableView class which as the AutoFormatRules exposed as properties
            // return if this cast cannot be done
            Outlook._TableView tv = olExplorer.CurrentView as Outlook._TableView;
            if (tv == null) return;
            // Try to find an existing AutoFormatRule for CRM Tracking and delete it
            // Use of the loop is necessary as there is no delete method on the AutoFormatRule object
            // So it's necessary to use the Remove method of a collection object instead
            for (int n = tv.AutoFormatRules.Count; n > 1; n--)
            {
                if (tv.AutoFormatRules[n].Name == "CRM Tracking Pending")
                {
                    tv.AutoFormatRules.Remove(n);
                }
            }
            //Add a new rule and then configure it
            Outlook.AutoFormatRule afr = tv.AutoFormatRules.Add("CRM Tracking Pending");
            afr.Filter = "\"http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/crmLinkState\" = 1";
            afr.Font.Italic = true;
            afr.Font.Bold = true;
            afr.Font.Color = Outlook.OlColor.olColorGreen;
            afr.Enabled = true;
            // Save and apply the changes to the rule
            tv.Save();
            afr.Filter = "\"http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/crmLinkState\" = 1";
            tv.Apply();
            afr.Filter = "\"http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/crmLinkState\" = 1";
        }
}