最后,我们解决了!
解决方案是
1)删除要更新的项目context.RemoveItem(TargetData.EntryID);
(我们在 RDOMessage、MailItem、RDOFolder 和 MAPIFolder 上使用了一些抽象。但我认为,背后的原理很清楚。
2)添加新项目(WithComCleanup
来自VSTOContrib项目)
using (var msg = RDOSession.Resource.GetMessageFromMsgFile(pathToMSG)
.WithComCleanup())
{
msg.Resource.Move(context.RDOFolder);
msg.Resource.Save();
}
3) 将ItemAdd处理程序附加到RDOFolder或MAPIFolder,注意项目集合必须在类级别声明!为什么要添加项目?因为既没有RDOMail.OnModified
也没有RDOMail.OnMoved
提供 MailItem 检索所需的有效 EntryID。我们在获取时编写自定义 UserAttributes,并在ItemAdd
...中读取它们
//...
m_OwnItems = m_Folder.Items
m_OwnItems.ItemAdd += new MSOutlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);
//...
void Items_ItemAdd(object Item)
{
//Outlook 2010: Version 14, only Outlook 2010 supports `ClearSelection` and `AddToSelection`
if (Item is MSOutlook.MailItem && ApplicationController.Instance.ApplicationVersion.Major >= 14)
{
var mail = Item as MSOutlook.MailItem;
//Check that the added item is the one you added with GetMessageFromMsgFile
//...
if (m_Explorer.IsItemSelectableInView(mail))
{
m_Explorer.ClearSelection();
m_Explorer.AddToSelection(mail);
}
}
}
4) 完成了!Outlook 2010 的这种行为在整个开发过程中让我们很恼火......