事件处理程序未添加到新邮件项目
本文关键字:新邮件 项目 添加 程序 事件处理 | 更新日期: 2023-09-27 18:11:40
我正在尝试创建一个简单的Outlook 2010插件来响应新的附件事件。下面的代码只有在取消注释MessageBox时才有效。显示行。但删除后,似乎没有添加事件处理程序。关于程序流,我遗漏了什么,这意味着模态消息框会影响事件处理程序的位置?
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Application.Inspectors.NewInspector += Inspectors_NewInspector;
}
void Inspectors_NewInspector(Outlook.Inspector Inspector)
{
Outlook.MailItem mailItem = Inspector.CurrentItem as Outlook.MailItem;
if (mailItem != null)
{
if (mailItem.EntryID == null)
{
mailItem.BeforeAttachmentAdd += mailItem_BeforeAttachmentAdd;
//System.Windows.Forms.MessageBox.Show("Twice");
}
}
}
void mailItem_BeforeAttachmentAdd(Outlook.Attachment Attachment, ref bool Cancel)
{
Cancel = true;
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
引发事件的COM对象必须是活动的。在你的情况下,你正在使用多个点符号和编译器创建一个隐式变量;一旦该变量被垃圾收集,它将停止触发事件。对于邮件项目也是如此—您将需要捕获检查员。关闭事件并从_mailItems列表中删除项目;
public partial class ThisAddIn
{
private Inspectors _inspectors;
private List<MailItem> _mailItems = new List<MailItem>();
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
_inspectors = Application.Inspectors;
_inspectors.NewInspector += Inspectors_NewInspector;
}
void Inspectors_NewInspector(Outlook.Inspector Inspector)
{
Outlook.MailItem mailItem = Inspector.CurrentItem as Outlook.MailItem;
if (mailItem != null)
{
if (mailItem.EntryID == null)
{
_mailItems.Add(mailItem):
mailItem.BeforeAttachmentAdd += mailItem_BeforeAttachmentAdd;
//System.Windows.Forms.MessageBox.Show("Twice");
}
}
}
void mailItem_BeforeAttachmentAdd(Outlook.Attachment Attachment, ref bool Cancel)
{
Cancel = true;
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}