Outlook 2007捕获ReplyToAll事件的共享加载项

本文关键字:共享 加载项 事件 ReplyToAll 2007 捕获 Outlook | 更新日期: 2023-09-27 17:59:44

我正在使用VS 2010&点网框架2.0。我在Extensibility->Shared Add-ins for Outlook中创建了一个项目。我正在尝试捕获ReplyToAll事件它没有被解雇。请查看以下代码:

OnConnection方法

inspectors = applicationObject.Inspectors;                        
inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(inspectors_NewInspector);

void inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
    {
        mailItem = null;
        try
        {
            Outlook.NameSpace ns = Inspector.Session;
            Outlook.MAPIFolder inbox = ns.GetDefaultFolder(
              Outlook.OlDefaultFolders.olFolderInbox);
            foreach (object o in inbox.Items)
            {
                mailItem = o as Outlook.MailItem;
                if (mailItem != null)
                {
                    break;
                }
            }
            if (mailItem == null)
            {
                MessageBox.Show("Couldn't find a mail item.");
            }
            else
            {
                ((Outlook.ItemEvents_10_Event)mailItem).ReplyAll += new
                    Outlook.ItemEvents_10_ReplyAllEventHandler(Connect_ReplyAll);
            }                              
        }
        catch (Exception ex)
        {
            MessageBox.Show("asdgh"+ex.StackTrace);
        }        
    }

void Connect_ReplyAll(object Response, ref bool Cancel)
    {
        MessageBox.Show(Response+"Hello You have Clikced ReplyTOAll");
    }

但是调用了Connect_ReplyAll方法什么是错的?

正在工作但事件已注册的新代码

public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
    {
        try
        {
             applicationObject = (Outlook.Application)application;
            if (connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup)
            {
                OnStartupComplete(ref custom);
            }
            addInInstance = addInInst;
            inspectors = applicationObject.Inspectors;
            explorer = applicationObject.Explorers.Application.ActiveExplorer();
            explorer.SelectionChange += new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(explorer_SelectionChange);                                                
            inspectors.NewInspector += new 
                Outlook.InspectorsEvents_NewInspectorEventHandler(inspectors_NewInspector);               
        }
        catch(Exception ex)
        {
            MessageBox.Show(""+ex.StackTrace);
        }
        //((Microsoft.Office.Interop.Outlook.ItemEvents_10_Event)mailItem).Reply += new Microsoft.Office.Interop.Outlook.ItemEvents_10_ReplyEventHandler(ReplyToAllEvent);
    }
void explorer_SelectionChange()
    {
        try
        {
            Outlook.MailItem mailExplorer=null;
            mailTO = "";
            mailCC = "";
            mailBCC = "";
            foreach (object selectedItem in explorer.Selection)
            {
                mailExplorer = selectedItem as Outlook.MailItem;
                //MessageBox.Show("" + mailItem.EntryID.ToString());
                break;
            }               
            if (mailExplorer != null)
            {
                if (selectedItems.Contains(mailExplorer.EntryID.ToString()))
                {
                    selectedItems.Remove(mailExplorer.EntryID);
                    ((Outlook.ItemEvents_10_Event)mailExplorer).ReplyAll -= new Outlook.ItemEvents_10_ReplyAllEventHandler(Connect_ReplyAll);
                    ((Outlook.ItemEvents_10_Event)mailExplorer).Reply -= new Outlook.ItemEvents_10_ReplyEventHandler(Connect_Reply);                        
                }                    
                ((Outlook.ItemEvents_10_Event)mailExplorer).ReplyAll +=
                    new Outlook.ItemEvents_10_ReplyAllEventHandler(Connect_ReplyAll);
                ((Outlook.ItemEvents_10_Event)mailExplorer).Reply +=
                    new Outlook.ItemEvents_10_ReplyEventHandler(Connect_Reply);
                selectedItems.Add(mailExplorer.EntryID);
                mailTO = mailExplorer.To;
                mailCC = mailExplorer.CC;
                mailBCC = mailExplorer.BCC;
            }
        }
        catch(Exception ex)
        {                
            MessageBox.Show(""+ex.StackTrace);
        }                                 
    }

"一旦我用ReplyAll事件注册了邮件项。如果选择了同一邮件项,则事件会多次触发。"通过使用上面的代码解决了这个问题,但当我分离事件时,我会收到新的错误请帮我解决

我收到这个错误

Outlook 2007捕获ReplyToAll事件的共享加载项

您希望引发事件的COM对象必须是活动的。上面的代码循环遍历收件箱中的所有项目(哎哟!为什么?),并在每次迭代中使用相同的变量,从而删除以前的值
若要回复邮件,需要首先选择该邮件,因此您只需循环浏览所选项目(Explorer.Selection集合)。通过挂接Explorer.SelectionChanged事件来跟踪所选内容,在该事件处理程序中,循环浏览Explorer.Slection collation中的所有项目,并将它们放在您自己的List<MailItem>列表中。这样,对象将一直处于活动状态,直到您从列表中删除它们为止
更好的是,不用List<MailItem>列表,而是创建自己的包装器,将MailItem存储为私有成员,并在该包装器中挂接ReplyAll事件。这样,当事件触发时,您将知道是哪个MailItem对象引发了该事件。然后,可以将每个选定MailItem的包装器存储在List<MyMailItemWrapper>集合中。