Search code examples
excelvbaspreadsheet-protection

Workbook.Protect bug with toggling protection


When I call Thisworkbook.Protect in VBA the protection of the workbook actually toggles from the current workbook's protection status instead of just protecting the workbook.

Thisworkbook.Unprotect doesn't do this though.

Anyone know why this is the case or how to get around it?


Solution

  • Not sure if it is by design or a bug... If you provide the parameters, it works fine:

    ActiveWorkbook.Protect ,true,true
    

    or

    ActiveWorkbook.Protect Structure:= true
    

    Whichever you need/want. This way it won't turn the protection off, only on.

    To turn the protection off, you can use this (assumning you only turned on structure protection):

    ActiveWorkbook.Protect Structure:= false
    

    Parameters are:

    .Protect(Password, Structure, Windows)