2013-06-21 81 views
3

我有一個小的Outlook 2010的外接程序和需要限制在自定義文件夾的一些行動:展望VSTO - 文件夾限制

  • 我可以以某種方式阻止用戶重命名文件夾?
  • 是否有可能阻止用戶將任何項目移動到文件夾?我知道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

請看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 
    } 
} 
+0

非常感謝!連接到資源管理器文件夾開關爲我做了訣竅! – silent