Click here to Skip to main content
16,016,605 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im trying to protect and unprotect worksheets in workbook.
as im passing 6 to 7 sheets one by 1

Passing sheet name string

from list <string> source to sourcesheet
Sourcewb is
Workbook SourceWB = _excel.Workbooks.Open(SourcePath);

List of sheet names from the method to source
List<String> Source = GetAllSheetName(SourcePath);

worksheet1 is
WorkSheet1 = SourceWB.Worksheets[Sourcesheet.ToString()];


now unprotect is happening at the beginning of the code and working fine with conditions in between.

but protect code not working as the sheets not protecting


any solution or any other way!!

What I have tried:

foreach (string Sourcesheet in Source)
            {
                WorkSheet1.Protect(sheetPassword);
                
            }
Posted
Updated 5-Aug-22 1:57am
v4
Comments
0x01AA 4-Aug-22 14:16pm    
Did you saved the workbook? Otherwhise it will be not protected ;)
I mean after protecting the sheets you need to call SourceWB.Save();
Randomuser787 5-Aug-22 5:20am    
same as source to blank template

foreach (string Blanksheet in Blank)
{

WorkSheet2 = SourceWB.Worksheets[Blanksheet.ToString()];
WorkSheet2.Protect(sheetPassword);
BlankWB.Save();
}

BlankWB.Save();
MessageBox.Show("Template saved!!");
releaseObject(_excel);

this was my code ending still :-( its not protecting.

1 solution

Most probably you forgot to save the workbook. This works for me:
private void ProtectTest()
{
    string safeExcelFilePath = @"c:\temp\cp.excel";
    string excelFile1 = Path.Combine(safeExcelFilePath, "Book1.xlsx");

    // Excel Application
    Microsoft.Office.Interop.Excel.Application _excel = new _Excel.Application();

    // Open workbook
    Workbook workbook1 = _excel.Workbooks.Open(excelFile1);

    // Get a worksheet
    Worksheet worksheet1 = workbook1.Worksheets["Sheet1"];

    // Protect the worksheet
    worksheet1.Protect("pwd");

    // One needs to save the workbook to make protection permanent
    workbook1.Save();
}
 
Share this answer
 
Comments
Maciej Los 4-Aug-22 15:48pm    
5ed!
0x01AA 5-Aug-22 0:27am    
Thank you Maciej.
Randomuser787 5-Aug-22 5:34am    
I have tried this in my code where I'm having 6 sheets and ding like this but not worked yet.

foreach (string Sourcesheet in Source)
{

WorkSheet1 = SourceWB.Worksheets[Sourcesheet.ToString()];
WorkSheet1.Protect(sheetPassword);
SourceWB.Save();
}
0x01AA 5-Aug-22 6:59am    
Can please you try only this one:

SourceWB.Worksheets[1].Protect("pwd");
SourceWB.Save();

If after that the first sheet is not proteced, then there is something I'm not aware about yet.
If it is protected, then you do something wrong and you should post more of your code.
Randomuser787 5-Aug-22 7:53am    
done. i have passed sheet password as string it didn't work now I changed to object it is working now

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900