保护UserInterfaceOnly和EnableOutlining

我正在为Excel创build一个插件。 它正在保护工作簿中的工作表:

public static void ProtectSheetUi(_Worksheet sheet, string password) { sheet.EnableOutlining = true; sheet.Protect(password, Contents: true, UserInterfaceOnly: true, AllowFormattingCells: true, AllowFormattingColumns: true, AllowFormattingRows: true); } 

但问题是,当我closures并重新打开工作簿时,分组/取消分组不起作用。 这是因为UserInterfaceOnly不是持久的。 在工作簿级别,我可以在打开时再次取消保护和保护表格。 但是如何用插件来实现呢?

UserInterfaceOnly设置不保存closures工作簿时。 工作簿打开时,您将不得不重置它。 最好的做法是在Workbook_Open事件过程中。 所以你需要在你的加载项中处理。 例如( 从内存中执行此操作,请忽略拼写错误/语法错误

 private void ThisAddIn_Startup(object sender, System.EventArgs e) { this.Application.WorkbookOpen += new Excel.AppEvents_WorkbookOpenEventHandler(Application_WorkbookOpen); } void Application_WorkbookOpen(Excel.Workbook Wb) { //~~> Rest of your code }