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

我正在使用VS 2010和Dot Net Framework 2.0。 我在Extensibility-> 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事件注册mailitem如果选择了相同的mailitem,那么事件会多次触发。” 这个问题通过使用上面的代码解决,但我得到新的错误,当我分离事件请帮助我

我收到了这个错误

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