我尝试在文件附件到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中创建一个新的电子邮件时,我的代码通过此方法传递,但是当我向我的电子邮件添加附件时,事件永远不会被触发。
有什么想法吗?
答案 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;
}
}