3

我有一个小的 Outlook 2010 插件,需要限制对自定义文件夹的一些操作:

  • 我可以以某种方式阻止用户重命名文件夹吗?
  • 是否可以阻止用户将任何项目移动到文件夹中?我知道 ItemAdd 事件,但这仅在项目已移动后才会触发。这对我来说太晚了。
  • 是否可以禁用文件夹的上下文菜单(右键单击)?

非常感谢!

// 编辑:我想我找到了上下文菜单问题的解决方案。这样做可以吗?或者这样做有缺陷吗?

outlook.FolderContextMenuDisplay += DisableArchiveFolderContextMenu;
private void DisableArchiveFolderContextMenu(Office.CommandBar commandBar, MAPIFolder folder)
{
  if (folder.Name.Equals(Settings.Default.ArchiveFolderName))
  {
    // Disable the context menu
    commandBar.Enabled = false;
  }
  else
  {
    commandBar.Enabled = true;
  } 
}
4

1 回答 1

4

查看如何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
    }
}
于 2013-06-21T21:13:38.443 回答