Protect UserInterfaceOnly and EnableOutlining

I am creating addin for Excel. He protects the sheets in the book:

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

But the problem is that when I close and open the book again, grouping / ungrouping does not work. This is because UserInterfaceOnly is not persistent. At the level of the workbook, I could again protect and protect the sheets when it opens. But how to achieve this with an add-in?

+3
source share
1 answer

Settings are UserInterfaceOnlynot saved when the book is closed. You will need to reset when the workbook is open. The best place for this is in the event procedure Workbook_Open. So you need to handle this in your add-in. For example (by doing this from memory, please ignore typos / syntax errors)

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
}
+2
source

All Articles