TaskRequestItem 传递给 Outlook 应用程序时丢失 UserProperties(VSTO 插件)

Lose UserProperties of TaskRequestItem when it is passed to Outlook Application (VSTO Addin)

我通过 .NET Outlook Interop 创建了一个 TaskRequestItem API 并添加了我自己的 UserProperty。 (我使用已经 运行 的 Outlook 实例) 最后我在 Outlook 中显示该项目,第一个过程结束。

在我的 Outlook 中是 运行 一个 VSTO 插件,它侦听事件 ItemSend。 如果现在我在 Outlook 中发送 TaskRequestItem,我无法在该项目的 Event ItemSend 中找到任何 UserProperties!

我还尝试了 ItemProperties 和 PropertyAccessor。 同样的问题,所有属性都消失了!

如果我用 MailItem 试试这个,那就行了!

首先添加属性,因为我可以在第 1 步添加它们后立即读出它们...

第 1 部分使用互操作创建新项目api:

Microsoft.Office.Interop.Outlook.Application outlook = Marshal.GetActiveObject("Outlook.Application") as OutlookApplication;

...

TaskItem item = outlook.CreateItem(OlItemType.olTaskItem);
var task = item.Assign();
var guid = Guid.NewGuid().ToString();
var userProperty = item.UserProperties.Add("My GUID", OlUserPropertyType.olText, true);
userProperty.Value = guid;
task.Save();
task.Display();

...

第 2 部分 VSTO 插件中的 ItemSend

private void MyAddIn_ItemSend(object item, ref bool cancel)

...

var oItm = item as TaskRequestItem;
foreach (UserProperty property in oItm.UserProperties)
{
    if (property.Name == "My GUID")
    {
        Console.WriteLine("My GUID: " + property.Value);
    }
}

任务本身永远不会发送 - 它保留在任务文件夹中,所有用户属性都完好无损。发送的是一个 TaskRequestItem 对象,它与原来的 lask 对象不同。您可以调用 TaskRequestItem.GetAssociatedTask 来检索原始任务对象并复制您的自定义属性。