PDA

View Full Version : Solved: How to unprotect



YellowLabPro
09-19-2006, 05:03 AM
If the following code will protect, how do I reverse this?



Sub PasswordProWs()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.Protect Password:="mypassword"
Next
End Sub


Thanks,

YLP

mdmackillop
09-19-2006, 05:07 AM
Would you believe

ws.Unprotect Password:="mypassword"

Woof!

YellowLabPro
09-19-2006, 05:11 AM
Thanks Malcolm,
I looked in the autosense and this did not pop up.... so I thought that would be way too ez. I just went back and tried it again. Maybe I did not give autosense enough room or something to operate correctly.

Nice picture... he looks tough.... a good strong Scottish Woofhound

now Yelp really is appropriate

YellowLabPro
09-19-2006, 05:17 AM
Malcolm,
In this instance, can this be set up to toggle. John W. has an example in his book that toggels Display Gridlines, but that uses something like this...

ActiveWindow.DisplayGridlines = Not ActiveWidnow.DisplayGirdlines

Bob Phillips
09-19-2006, 05:55 AM
Not directly. The GridLines example works because it has a Boolean value, which toggles nicely. For methods you wwould need to test if it is protected, if so Unprotect, else Protect. But it seems to me that the logic should know, so no need to test.

mdmackillop
09-19-2006, 06:00 AM
Nice picture... he looks tough.... a good strong Scottish Woofhound
A Rhodesian Ridgeback, sadly no longer with us.

YellowLabPro
09-19-2006, 06:13 AM
xld-
This may be a "stupid question" but how is the gridlines a boolean value and the protect/unprotect not? Is this defined in the property already or is it where I need to recognize this of the property type?

YellowLabPro
09-19-2006, 06:14 AM
Malcolm,
Sorry to hear that. He was a handsome fellow, that is for sure.

Bob Phillips
09-19-2006, 07:38 AM
xld-
This may be a "stupid question" but how is the gridlines a boolean value and the protect/unprotect not? Is this defined in the property already or is it where I need to recognize this of the property type?

DisplayGridlines is a Read/write Boolean property of the Window object, so you set it by setting the value to True or False. You can test if it is set with



If Activewindow.DisplayGridlines = True Then


but because a condition evaluates to True or False you can shorten that to



If Activewindow.DisplayGridlines Then


which also means that you can test the value and reverse it in one statement



If Activewindow.DisplayGridlines = Not Activewindow.DisplayGridlines


However ... Protect and Unprotect are methods associated with the worksheet object. You don't test if the Protect property is set (there isn't one), you test the various properties. Thus



If ActiveSheet.Protect Then


whilst it will not fail, will just return empty, so you cannot possibly do



ActiveSheet.Protect = Not ActiveSheet.Protect


Properties and methods are very different. Properties are an attribute of the object, methods are actions that can be done to an object.

YellowLabPro
09-19-2006, 08:56 AM
Thanks xld....
makes great sense

ylp

Prasad_Joshi
09-21-2006, 02:55 AM
Why ws.Unprotect Password:="mypassword" ?
Use
ws.Unprotect "mypassword"

mdmackillop
09-21-2006, 05:49 AM
Why ws.Unprotect Password:="mypassword" ?
Use
ws.Unprotect "mypassword"
Based on the OP's original question, my method required only the insertion of the letters "Un" to his code, which seemed like the simplest soution.

Bob Phillips
09-21-2006, 06:24 AM
Why ws.Unprotect Password:="mypassword" ?
Use
ws.Unprotect "mypassword"

Because it is a good practice to use the argument keywords.