查看如何BeforeItemMove
防止用户将项目移动到您的文件夹中。此事件需要附加到您有兴趣观看的每个文件夹 -BeforeItemMove
我不知道没有全局。您将不得不递归地迭代每个邮箱文件夹或锁定Explorer.FolderSwitch
并附加您的项目侦听器。但它FolderSwitch
有一些限制,因为您仍然可以在不通过 Inspector Ribbon、OWA 或 EWS 切换文件夹的情况下移动项目。
为了防止用户重命名您的文件夹,您可以使用StorageItem
来保留通过FolderChange
文件夹的上下文父级实现的文件夹名称。
public partial class ThisAddIn
{
Outlook.Folder folder;
Outlook.Folder customFolder;
Outlook.Folders mailbox;
Outlook.Explorer explorer;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
explorer = Globals.ThisAddIn.Application.ActiveExplorer();
explorer.FolderSwitch += new Outlook.ExplorerEvents_10_FolderSwitchEventHandler(explorer_FolderSwitch);
mailbox = Globals.ThisAddIn.Application.Session.DefaultStore.GetRootFolder().Folders;
if (mailbox.Cast<Outlook.Folder>().Where(c => c.Name == "Custom Folder").Count() == 0)
{
customFolder = mailbox.Add("Custom Folder") as Outlook.Folder;
Outlook.StorageItem si = customFolder.GetStorage("Custom Folder Storage", Outlook.OlStorageIdentifierType.olIdentifyBySubject);
si.UserProperties.Add("PermanentFolderName", Outlook.OlUserPropertyType.olText).Value = customFolder.Name; // store persistent name
si.Save();
}
else
customFolder = mailbox["Custom Folder"] as Outlook.Folder;
mailbox.FolderChange += new Outlook.FoldersEvents_FolderChangeEventHandler(mailbox_FolderChange);
}
void explorer_FolderSwitch()
{
folder = explorer.CurrentFolder as Outlook.Folder; // grab new handle
folder.BeforeItemMove += new Outlook.MAPIFolderEvents_12_BeforeItemMoveEventHandler(folder_BeforeItemMove);
}
void mailbox_FolderChange(Outlook.MAPIFolder Folder)
{
Outlook.Folder folder = Folder as Outlook.Folder;
Outlook.StorageItem si = folder.GetStorage("Custom Folder Storage", Outlook.OlStorageIdentifierType.olIdentifyBySubject);
if (si.Size > 0 && si.UserProperties.Count > 0 && si.UserProperties["PermanentFolderName"].Value != folder.Name)
folder.Name = si.UserProperties["PermanentFolderName"].Value; // override users name change
}
void folder_BeforeItemMove(object Item, Outlook.MAPIFolder MoveTo, ref bool Cancel)
{
if (MoveTo.Name == "Custom Folder")
Cancel = true; // disallow moving items here
}
}