use*_*945 1 outlook vsto outlook-addin
我正在尝试以编程方式将自动格式规则应用于 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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
规则已创建,但未应用过滤器值。
建议之一是过滤器值必须以“@SQL=....”为前缀。但这是行不通的。
然后我发现这个主题Outlook 2010 AutoFormatRule.Filter property not saving
回应是:
我针对这个问题提出了首要呼吁。答复是这是 Outlook 对象模型中的一个已知错误。它不会在 Outlook 2010 或 Outlook 2013 中修复,因为对于小更改来说风险太大。
建议的解决方法是:
Microsoft 提供的解决方案是将规则从公共文件夹复制到用户的配置文件。
这意味着什么?是否有其他解决方法可以使规则在 C# 代码中起作用?
小智 5
这个问题有一个解决方案,尽管有点混乱。这绝对是一个 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";
}
}
Run Code Online (Sandbox Code Playgroud)