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

时间:2013-02-16 14:11:39

标签: c# .net outlook add-in outlook-addin

我正在使用VS 2010& Dot Net Framework 2.0。 我在Extensibility中创建了一个项目 - > Outlook的共享加载项。 我试图捕获ReplyToAll事件它没有被解雇。 请查看以下代码:

OnConnection Method

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事件注册mailitem如果选择了相同的mailitem,那么事件会多次触发。”通过使用上面的代码解决了这个问题,但是当我分离事件时我遇到了新的错误 请帮帮我

我收到此错误

1 个答案:

答案 0 :(得分:2)

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