none
Unprotect error during Style settings RRS feed

  • Question

  • I get the following error at the statement "style.Font.Name = Properties.Resources.FontStyle;" :

    You cannot use this command on a protected sheet. To use this command, you must first unprotect the sheet (Review tab, Changes group, Unprotect Sheet button). You may be prompted for a password.

     internal static void CreateModuleSheetDesign(Excel.Worksheet newWorksheet, ModuleSheetDescription sheetDescription)
            {
                Globals.ThisWorkbook.Unprotect(Properties.Resources.PasswordProtect);
    
                foreach (Microsoft.Office.Interop.Excel.Worksheet workSheet in Globals.ThisWorkbook.Worksheets)
                {
                    workSheet.Unprotect(Properties.Resources.PasswordProtect);
                }
                            
                Excel.Style style = Globals.ThisWorkbook.Styles.Add(newWorksheet.Name + Properties.Resources.StyleMain);
                style.Font.Name = Properties.Resources.FontStyle;
    
    //More code
    
    }

    I have unprotected the workbook and all the sheets. But I get the error.

    Project Type : Excel 2010 WorkBook (Document Level Customization)

    Thursday, October 24, 2013 9:23 AM

Answers

  • Hi,

    According to your description, you want to add a new style to the Workbook. You are troubleshooting with the code to unprotect the Workbook and Worksheet to in avoid an error.

    Through you said “But I get the error.”, it works fine when I run your code to add a new style to a protected Workbook. So which row of the code causes the new error and could you share the error message to help us to reproduce your issue?

    You could check the workbook to make sure whether the Workbook is unprotected after you delete the operation of adding the style and running the code. If the Workbook is unprotected, you could try to add the style to the workbook manually by clicking the More button below. If you could not do that successfully, I think the error may be related to some specify setting in your Workbook.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 25, 2013 10:14 AM
    Moderator

All replies

  • Hi,

    According to your description, you want to add a new style to the Workbook. You are troubleshooting with the code to unprotect the Workbook and Worksheet to in avoid an error.

    Through you said “But I get the error.”, it works fine when I run your code to add a new style to a protected Workbook. So which row of the code causes the new error and could you share the error message to help us to reproduce your issue?

    You could check the workbook to make sure whether the Workbook is unprotected after you delete the operation of adding the style and running the code. If the Workbook is unprotected, you could try to add the style to the workbook manually by clicking the More button below. If you could not do that successfully, I think the error may be related to some specify setting in your Workbook.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 25, 2013 10:14 AM
    Moderator
  • Hi, 

    Since we haven't heard from you for a long time, I temporarily close this case. I mark useful reply as answer. If you have any concerns, please free feel to reopen it or submit a new question.

    Thanks for your understanding. 

    Best regards, 
    Luna


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, October 31, 2013 6:53 AM
    Moderator