I wan’t to protect a Excel document including all the sheets inside.
I’m using this procedure:
foreach(Worksheet sheetToProtect in excel.Worksheets)
sheetToProtect.Protect(ProtectionType.All, password, null);
So I first protect all the sheets one by one and afterwards I protect the document itself (all using the same password).
But on protecting the workbook I get an error saying the workbook is already protected. Is this because I first protected all the sheets? Maybe the first step is redundant because I always use ProtectionType.All?
Please post your template file here.
And what happens if you remove this line of code?
In the attached file I have the protection problems.
I also tried testing the protection first using:
The if-statement returns true (so no protection is detected) But he Protect method fails saying the workbook is already protected.
When I open the file in Excel I don’t see any protection (I can’t unprotect it in Excel).
Cycling through all the sheets and protecting them works fine. But I also want to protect the workbook so no new sheets can be added or exsisting ones can be altered.
Please try this attached fix.
I think it works now. Thanks.