C# C-希望在Outlook中添加/删除存储时捕获事件
我正在编写一个加载项,当通过数据文件管理菜单或AddStore/RemoveStore添加/删除PST时,需要记录该加载项。我一直很难找到关于如何捕捉这些事件的文档 非常感谢您的建议 谢谢, 拉里 编辑:这是我的伪代码,它不起作用:C# C-希望在Outlook中添加/删除存储时捕获事件,c#,event-handling,outlook-addin,outlook-2007,outlook-2007-addin,C#,Event Handling,Outlook Addin,Outlook 2007,Outlook 2007 Addin,我正在编写一个加载项,当通过数据文件管理菜单或AddStore/RemoveStore添加/删除PST时,需要记录该加载项。我一直很难找到关于如何捕捉这些事件的文档 非常感谢您的建议 谢谢, 拉里 编辑:这是我的伪代码,它不起作用: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; using Outlook = Microsof
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;
namespace StoreTesting
{
public partial class ThisAddIn
{
Outlook.Application olApp = new Outlook.ApplicationClass();
Outlook.NameSpace ns;
Outlook.Stores stores;
int open;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
ns = olApp.GetNamespace("MAPI");
stores = ns.Stores;
open = 0;
foreach (Outlook.MAPIFolder mf in stores.Session.Folders)
if (mf.Store.IsDataFileStore)
open += 1;
stores.StoreAdd += new Outlook.StoresEvents_12_StoreAddEventHandler(stores_StoreAdd);
stores.BeforeStoreRemove += new Outlook.StoresEvents_12_BeforeStoreRemoveEventHandler(stores_BeforeStoreRemove);
}
void stores_BeforeStoreRemove(Outlook.Store Store, ref bool Cancel)
{
string rf = string.Format("{0}:{1} was removed", Store.DisplayName);
MessageBox.Show(rf);
open -= 1;
}
void stores_StoreAdd(Outlook.Store Store)
{
Outlook.MAPIFolder mf = ns.Folders.GetLast();
string af = string.Format("{0} was added", mf.Name);
MessageBox.Show(af);
open += 1;
}
void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}
Outlook会话对象有两个用于添加和删除pststore的事件-
StoreAdd
删除之前
无论何时添加或删除任何pst,您都可以订阅这些事件以获得认证。如下所示:
...
private void ThisAddInStartup(object sender, EventArgs e)
{
this.Application.Session.Stores.StoreAdd += StoreAddEventHandler;
this.Application.Session.Stores.BeforeStoreRemove += BeforeStoreRemove;
}
private void StoreAddEventHandler(Store store)
{
if (store.IsDataFileStore)
{
//Do something.
}
}
private void BeforeStoreRemove(Store store, ref bool cancel)
{
if (store.IsDataFileStore)
{
//Do something.
}
}
…我已经编写了一个虚拟代码程序来测试这一点,但只有通过AddStore/RemoveStore函数和Open->Outlook Data File菜单选项才能获得成功。Kaplig:我已经订阅了这些事件,但是在使用DFM菜单时没有发生任何事情。我会发布我的虚拟代码,这样你就可以看到我指的是什么。谢谢,MLJ。几个月前我解决了这个问题。这只是我的打字错误。