How does protection work?

Hi

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);

excel.Protect(ProtectionType.All, password)

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?

Stief

Please post your template file here.

And what happens if you remove this line of code?

excel.Protect(ProtectionType.All, password)

Laurence

In the attached file I have the protection problems.

I also tried testing the protection first using:
if (!excel.IsProtected)
excel.Protect(ProtectionType.All, password);

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.

Stief

Hi Stief,

Please try this attached fix.

Laurence

I think it works now. Thanks.

Stief