首页 > 解决方案 > Outlook 项目导致 AftwerWrite 事件不可用

问题描述

我正在开发 Outlook 加载项。我的目标是让 EntryId 分配给刚刚保存的 Outlook 项目,特别是任务项目。所以,在适当的包装类中,我有:

public TaskItemEventWrapper(Outlook.TaskItem item)
        {
            Item = item;
            Id = new Guid();
            Item.BeforeRead += new Outlook.ItemEvents_10_BeforeReadEventHandler(Item_BeforeRead);
            Item.Read += new Outlook.ItemEvents_10_ReadEventHandler(Item_Read);
            Item.Unload += new Outlook.ItemEvents_10_UnloadEventHandler(Item_Unload);
            Item.AfterWrite += Item_AfterWrite;
            Item.AttachmentAdd += Item_AttachmentAdd;
            Item.AttachmentRead += Item_AttachmentRead;
            Item.AttachmentRemove += Item_AttachmentRemove;
            Item.BeforeAttachmentAdd += Item_BeforeAttachmentAdd;
            Item.BeforeAttachmentPreview += Item_BeforeAttachmentPreview;
            Item.BeforeAttachmentRead += Item_BeforeAttachmentRead;
            Item.BeforeAttachmentSave += Item_BeforeAttachmentSave;
            Item.BeforeAttachmentWriteToTempFile += Item_BeforeAttachmentWriteToTempFile;
            Item.BeforeAutoSave += Item_BeforeAutoSave;
            Item.BeforeCheckNames += Item_BeforeCheckNames;
            Item.BeforeDelete += Item_BeforeDelete;
            Item.CustomAction += Item_CustomAction;
            Item.CustomPropertyChange += Item_CustomPropertyChange;
            Item.Open += Item_Open;
            Item.PropertyChange += Item_PropertyChange;
            Item.ReadComplete += Item_ReadComplete;
            Item.Write += Item_Write;
        }
void Item_AfterWrite()
{
    System.Diagnostics.Debug.WriteLine("Id --> " + Item.EntryID); 
}
void Item_Write(ref bool Cancel)
{
    if (!Cancel)
    {                
        System.Diagnostics.Debug.WriteLine("Id --> " + Item.EntryID);
    }
}

如果我尝试在 Write 事件中检查 EntryId,我会得到空值。如果我尝试在 After_Write 事件中检查 EntryId,则会收到错误消息:

1错误互操作

所以,我的问题是:何时何地是获取分配给项目的新 EntryId 的正确位置?谢谢!

标签: c#outlookinteropoutlook-addin

解决方案


您只能在该事件中访问Class和属性。MessageClass最好的办法是在事件处理程序中启用计时器(使用 Forms 命名空间中的 Timer 类,因为它使用主线程)。当计时器触发时,您将脱离事件处理程序并能够访问任何MailItem属性。


推荐阅读