添加附件事件Outlook AddIn

时间:2017-04-03 16:04:10

标签: c# outlook com ms-office outlook-addin

我尝试在文件附件到mailItem之前将outlook文件放在outlook插件中。

    private void Inspectors_NewInspector(Outlook.Inspector Inspector)
        {

            if (Inspector.CurrentItem is Outlook.MailItem)
            {

    Outlook.MailItem mail = (Outlook.MailItem)Inspector.CurrentItem;
                    Inspector.AttachmentSelectionChange += Inspector_AttachmentSelectionChange;
                    Application.AttachmentContextMenuDisplay += Application_AttachmentContextMenuDisplay;
                    mail.BeforeAttachmentAdd += Mail_BeforeAttachmentAdd;
                    mail.AttachmentAdd += Mail_AttachmentAdd;
                    mail.BeforeAttachmentWriteToTempFile += Mail_BeforeAttachmentWriteToTempFile;
                    mail.BeforeAttachmentSave += Mail_BeforeAttachmentSave;
}}

当我在outlook中创建一个新的电子邮件时,我的代码通过此方法传递,但是当我向我的电子邮件添加附件时,事件永远不会被触发。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您需要在类级别(全局范围)声明源对象,以防止它被垃圾回收器忽略,例如:

    Outlook.MailItem mail = null;
    Outlook.Inspector inspector = null;

    private void Inspectors_NewInspector(Outlook.Inspector Inspector)
    {
        inspector = Inspector;
        object oMail = inspector.CurrentItem;
        if (oMail is Outlook.MailItem)
        {

                mail = (Outlook.MailItem)oMail.CurrentItem;               
                inspector.AttachmentSelectionChange += Inspector_AttachmentSelectionChange;
                Application.AttachmentContextMenuDisplay += Application_AttachmentContextMenuDisplay;
                mail.BeforeAttachmentAdd += Mail_BeforeAttachmentAdd;
                mail.AttachmentAdd += Mail_AttachmentAdd;
                mail.BeforeAttachmentWriteToTempFile += Mail_BeforeAttachmentWriteToTempFile;
                mail.BeforeAttachmentSave += Mail_BeforeAttachmentSave;
        }
    }