I want to create a command button that will unprotect a document and another to protect it again. Is it possible to then have the command buttons not show when I print the document?
Kerry
![]() |
There are isolated problems with current patches, but they are well-known and documented on this site. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Command Button to Unprotect Document (Word 97)
Hi Kerry
As Hans says, the functionality is already there, but do you need the code for your own custom incarnation? Rather than two buttons, you could use just one with a caption that toggles Protect/ Unprotect depending on the current value of the Protect property of the document.
As far a preventing a button from printing, I don’t think there’s a built-in property to control this – a bit of an oversight IMO if this is the case. There is a workaround here that might do the trick.
Alan
Thanks for the replies. I should have been more specific. My apologies to all.
I am making Forms for our workers who have a varying range of computer literacy. I am making forms with fields and protecting them simply to make it easy to tab through the document and fill in the fields. Sometimes a form needs to be unlocked for a few different reasons which I wont go into now. Unfortunately out of the hundreds of people that will use these forms, many dont know how to even find the Forms toolbar! It is easier to make the form do the work, than try and teach all of these people about Word. Hence my idea of putting command buttons on the form itself. I dont want it to be global.
I took a look at the workaround you mention and it looks good. I would appreciate assistance to do exactly as the example does, except not to Print, but to protect and unprotect the document.
Hope this makes things a little clearer.
Kerry
I figured you wanted an “in-your-face” button, right in the document body, for the less savvy users. I tried some relatively simple code to set up a button (named btnProtect) operation, but have run into a problem I don’t understand. Maybe somebody more familiar with Word VBA will be able to identify the problem. Here is the code:
Private Sub Document_Open()
If ActiveDocument.ProtectionType wdNoProtection Then
btnProtect.Caption = “Unprotect”
Else
btnProtect.Caption = “Protect”
End If
End Sub
Private Sub btnProtect_Click()
Dim Doc As Document
Set Doc = ActiveDocument
With Doc
If .ProtectionType wdNoProtection Then
‘Doc is protected so remove protection
.Unprotect
btnProtect.Caption = “Protect”
Else
.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
‘ Runtime error 6461 – The ToolsProtectDocument statement is currently disabled.
btnProtect.Caption = “Unprotect”
End If
End With
End Sub
I’ve not looked at the printing issue and I think the events might be different for 97 and 2000. An easy way might be to exclude the printing of drawing objects – obviously not suitable if you do have some you need printing though. I’ll keep thinking about this though.
Alan
(Edited by HansV to make URLs clickable – see Help 19)
Hi Alan
I have collected a few issues I really want to address in one form.
Firstly – users are unhappy with the loss of the spellchecking facility on Forms.
http://www.mvps.org/word/FAQs/MacrosVBA/Sp…kProtectDoc.htm%5B/url%5D
Secondly – when a form/template is unprotected and then reprotected they loose all information.
http://www.mvps.org/word/FAQs/MacrosVBA/Tu…FlfResetOff.htm%5B/url%5D
Thirdly – I want to tie all this together with a button on the form that will not show up when I print the document.
http://www.mvps.org/word/FAQs/TblsFldsFms/…PrintButton.htm%5B/url%5D
I never do things in small measures! Just a shame I know what I want to do, but rarely do I know how!
I have attached a form with the first two lots of code in, but I need major help in putting it all together. Once I get one form to work, I can use it for the basis of any further forms.
Any help is always greatly appreciated.
Kerry
Kerry,
Is this something like you are looking for? I added a toolbar which stays with the template and corresponding document but the toolbar has 2 macros – one for Word 97 and one for Word 2000 and up (I couldn’t get the one for 2000 and up to work with 97). If you don’t have 2000 or higher, that icon can be pulled from the toolbar.
By selecting it, it unprotects the documents, spell checks form fields, then reprotects the document without losing any information.
Leanne you are a genius!
Is it also possible to make a button on the toolbar to protect and unprotect, since as well as spell check, they will want to unprotect a document and then reprotect it as a separate process.
Just so I am learning something here, does your macro call anything from the code I had in the “This Document” object? I am still trying to come to terms with the difference between modules etc. (There is tooooo much to learn)
Kerry
Kerry
I hadn’t realized that a custom toolbar was within your bounds of an acceptable solution. This is a better alternative to a button on the form for you, since there’s no issue with it printing. If this is the case, you can easily include any of your own macros on it. If your template is occassionally revised and redistributed, you’ll be better off “hard coding” the toolbar into the template, so that it is freshly created on the user’s system each time, as required – otherwise there can be problems with existing older versions.
It doesn’t appear that Leanne has used any of your macro code on the toolbar macros she included, and as said, you can include any of your own macros thereon. Is this (custom toolbar) the kind of solution you’re after?
Alan
There are two approaches to creating custom commandbars. One is to do it from the user interface via Tools -> Customize -> Toolbars. If you keep this dialog open and have a “play” with this feature, you’ll see that you can create the kind of toolbar Leanne added – right click any of the items (on any toolbar in fact) and you’ll see their properties etc. You can pull items off the toolbar, drag new ones (including your own macros) on etc. I think this is much the same as Excel, with which I’m more familiar. The problem is (with Excel distributions anyway) that “old” copies of custom commandbars can be stored on the user’s system and tend to “stick”, not allowing new/ revised versions to (easily) replace them… this has been my experience anyway. This problem also seems to vary for user to user.
The other way is to do the whole thing from within VBA. When the template is opened, or a new doc is created, the commandbar is created along with it. Upon closing, the commandbar is deleted. This relies on using the “special” macros AutoNew , AutoOpen & AutoClose . The first two would call a procedure to create the commandbar if it doesn’t exist already, while the third would call a procedure to delete it.
I’ve given a quick (and decidedly rough ) example in the attachment. All the code is in the module cmBarDemo. It attaches two of your own macros to the new commandbar called “Custom”. Just as a separate issue, I’d consider putting all of you own code into a module of its own, rather than in the ThisDocument module, which is really designed for quite specific porposes. Hope this gives some helpful ideas, but the subject of commandbars is quite a broad topic, so repost with any specific queries.
Alan
Thanks Alan. You example is great. I am learning – bit by bit. I managed to find the correct FaceID s at http://www.mvps.org/skp/faceid.htm.
I have moved the macros into modules as you suggested. I have attached what I have done.
One thing that has me wondering, is, on the normal Forms toolbar, the Padlock Icon toggles up and down when pressed, and yet it does not on the custom toolbar. Is this able to be done on the custom toolbar?
I have another question regarding which spell checking macro to use, but I think I should post that as a new question as this is getting a bit off the original topic. Do you agree?
Thanks
Kerry
As far as toggling the padlock icon goes, this is something you’d have to write into your protect/ unprotect procedures… but it’s not that hard to do. It follows something like I tried to show in post 329047, where the button label toggled depending on the protection state. I guess you’d want a single toolbar button that reads “Protect” with a locked padlock, when the document is unprotected, and reads “Unprotect” with an open padlock when the document is protected. For the case of using such a single button, I suggested something like this:
If .ProtectionType wdNoProtection Then
‘Doc is protected so remove protection
.Unprotect ‘ or call your Unprotect procedure here
btnProtect.Caption = “Protect”
Else
.Protect Type:=wdAllowOnlyFormFields, NoReset:=True ‘ or call your Protect procedure here
btnProtect.Caption = “Unprotect”
End If
You’d have to just substitute lines like:
CommandBars(“Custom”).Controls(1).FaceId = 108 ‘ Use the ID for the required padlock
CommandBars(“Custom”).Controls(1).Caption = “Protect”
instead of the btnProtect lines. There are a few FaceID “browsers” around – I guess you’ve found one for Word already.
Also, with the way you’ve organized your own procedures, it might be best (at first anyway) to keep them all in just the one single module. “Private” macros can be accessed only from their containing module, so you might run into problems if you split up interdependent routines.
As to a separate thread? Your guess is as good as mine. I’ll have a look at your new version ASAP.
Alan
Edited – Sorry, I misinterpreted the padlock situation. I believe what you’re after is control over the up/down appearance of the button. The appropriate code for each state would be:
CommandBars(“Custom”).Controls(2).State = msoButtonUp
CommandBars(“Custom”).Controls(2).State = msoButtonDown
so these lines would go in place of the .FaceId lines above. The FaceId remains constant (I couldn’t find an unlocked padlock – must have been thinking of web browser icons).
Sorry, my input here has been a bit of hotchpotch, mixing my code samples with your existing procedures. In essence, the module I inserted is concerned only with setting up and deleting the Custom commandbar. It assigns a procedure called ProtectForm to the button designated Controls(2). I’m not sure if that procedure works – you may want to use your ToolsProtectUnprotectDocument instead, or whichever toggling procedure you’ve found works.
Either way, the appropriate changes to the button should be made after the protection state is changed, to reflect the newly toggled functionality of the button. Using the latter procedure as the guinnea pig:
If oDoc.ProtectionType = wdNoProtection Then
With Dialogs(wdDialogToolsProtectDocument)
.NoReset = True
.Show
End With
‘ At this point the document has become protected (I guess??)
‘ So reset the button to reflect its protected state and its new function – turning protection OFF
CommandBars(“Custom”).Controls(2).Caption = “Unprotect”
CommandBars(“Custom”).Controls(2).State = msoButtonDown
Else
oDoc.Unprotect
‘ Similarly…
CommandBars(“Custom”).Controls(2).Caption = “Protect”
CommandBars(“Custom”).Controls(2).State = msoButtonUp
End If
I hope this gives a better idea.
Alan
Alan
I think I have it all worked out now. I decided not to go with the custom toolbar as it did some wierd things. It kept on appearing on new documents and multiple times even. I hope you are not offended – I really appreciate your help.
I have only kept the two spellchecking macros – 97 and 2000 just incase some users have higher versions. I have kept the protection routine that I found on the MVPS site which stops the data from resetting when the form is unlocked and locked again.
As for the Lock and Unlock button on the Spellcheck Toolbar, I simply put the Word default one there and it toggles fine.
In all – I have learnt that I can use toolbars for the job and how to assign macros and icons to these toolbars. Also to keep all the macros in modules.
Thankyou Alan and everyone else for your assistance. This Forum is the greatest!
I have attached the final version for anyone interested in seeing the final result of this thread.
Kerry
No problem. As I said, my code was a bit on the rough & ready side. The weird toolbar behaviour was probably because I omitted to check if the commandbar already existed before trying to create it. There may also be a problem with the code deleting it if it’s being used by another document. I’m sure these problems are soluble though, but it’s also good to go with what works at the time.
Alan
No problem. As I said, my code was a bit on the rough & ready side. The weird toolbar behaviour was probably because I omitted to check if the commandbar already existed before trying to create it. There may also be a problem with the code deleting it if it’s being used by another document. I’m sure these problems are soluble though, but it’s also good to go with what works at the time.
Alan
Alan
I think I have it all worked out now. I decided not to go with the custom toolbar as it did some wierd things. It kept on appearing on new documents and multiple times even. I hope you are not offended – I really appreciate your help.
I have only kept the two spellchecking macros – 97 and 2000 just incase some users have higher versions. I have kept the protection routine that I found on the MVPS site which stops the data from resetting when the form is unlocked and locked again.
As for the Lock and Unlock button on the Spellcheck Toolbar, I simply put the Word default one there and it toggles fine.
In all – I have learnt that I can use toolbars for the job and how to assign macros and icons to these toolbars. Also to keep all the macros in modules.
Thankyou Alan and everyone else for your assistance. This Forum is the greatest!
I have attached the final version for anyone interested in seeing the final result of this thread.
Kerry
Sorry, my input here has been a bit of hotchpotch, mixing my code samples with your existing procedures. In essence, the module I inserted is concerned only with setting up and deleting the Custom commandbar. It assigns a procedure called ProtectForm to the button designated Controls(2). I’m not sure if that procedure works – you may want to use your ToolsProtectUnprotectDocument instead, or whichever toggling procedure you’ve found works.
Either way, the appropriate changes to the button should be made after the protection state is changed, to reflect the newly toggled functionality of the button. Using the latter procedure as the guinnea pig:
If oDoc.ProtectionType = wdNoProtection Then
With Dialogs(wdDialogToolsProtectDocument)
.NoReset = True
.Show
End With
‘ At this point the document has become protected (I guess??)
‘ So reset the button to reflect its protected state and its new function – turning protection OFF
CommandBars(“Custom”).Controls(2).Caption = “Unprotect”
CommandBars(“Custom”).Controls(2).State = msoButtonDown
Else
oDoc.Unprotect
‘ Similarly…
CommandBars(“Custom”).Controls(2).Caption = “Protect”
CommandBars(“Custom”).Controls(2).State = msoButtonUp
End If
I hope this gives a better idea.
Alan
As far as toggling the padlock icon goes, this is something you’d have to write into your protect/ unprotect procedures… but it’s not that hard to do. It follows something like I tried to show in post 329047, where the button label toggled depending on the protection state. I guess you’d want a single toolbar button that reads “Protect” with a locked padlock, when the document is unprotected, and reads “Unprotect” with an open padlock when the document is protected. For the case of using such a single button, I suggested something like this:
If .ProtectionType wdNoProtection Then
‘Doc is protected so remove protection
.Unprotect ‘ or call your Unprotect procedure here
btnProtect.Caption = “Protect”
Else
.Protect Type:=wdAllowOnlyFormFields, NoReset:=True ‘ or call your Protect procedure here
btnProtect.Caption = “Unprotect”
End If
You’d have to just substitute lines like:
CommandBars(“Custom”).Controls(1).FaceId = 108 ‘ Use the ID for the required padlock
CommandBars(“Custom”).Controls(1).Caption = “Protect”
instead of the btnProtect lines. There are a few FaceID “browsers” around – I guess you’ve found one for Word already.
Also, with the way you’ve organized your own procedures, it might be best (at first anyway) to keep them all in just the one single module. “Private” macros can be accessed only from their containing module, so you might run into problems if you split up interdependent routines.
As to a separate thread? Your guess is as good as mine. I’ll have a look at your new version ASAP.
Alan
Edited – Sorry, I misinterpreted the padlock situation. I believe what you’re after is control over the up/down appearance of the button. The appropriate code for each state would be:
CommandBars(“Custom”).Controls(2).State = msoButtonUp
CommandBars(“Custom”).Controls(2).State = msoButtonDown
so these lines would go in place of the .FaceId lines above. The FaceId remains constant (I couldn’t find an unlocked padlock – must have been thinking of web browser icons).
Thanks Alan. You example is great. I am learning – bit by bit. I managed to find the correct FaceID s at http://www.mvps.org/skp/faceid.htm.
I have moved the macros into modules as you suggested. I have attached what I have done.
One thing that has me wondering, is, on the normal Forms toolbar, the Padlock Icon toggles up and down when pressed, and yet it does not on the custom toolbar. Is this able to be done on the custom toolbar?
I have another question regarding which spell checking macro to use, but I think I should post that as a new question as this is getting a bit off the original topic. Do you agree?
Thanks
Kerry
There are two approaches to creating custom commandbars. One is to do it from the user interface via Tools -> Customize -> Toolbars. If you keep this dialog open and have a “play” with this feature, you’ll see that you can create the kind of toolbar Leanne added – right click any of the items (on any toolbar in fact) and you’ll see their properties etc. You can pull items off the toolbar, drag new ones (including your own macros) on etc. I think this is much the same as Excel, with which I’m more familiar. The problem is (with Excel distributions anyway) that “old” copies of custom commandbars can be stored on the user’s system and tend to “stick”, not allowing new/ revised versions to (easily) replace them… this has been my experience anyway. This problem also seems to vary for user to user.
The other way is to do the whole thing from within VBA. When the template is opened, or a new doc is created, the commandbar is created along with it. Upon closing, the commandbar is deleted. This relies on using the “special” macros AutoNew , AutoOpen & AutoClose . The first two would call a procedure to create the commandbar if it doesn’t exist already, while the third would call a procedure to delete it.
I’ve given a quick (and decidedly rough ) example in the attachment. All the code is in the module cmBarDemo. It attaches two of your own macros to the new commandbar called “Custom”. Just as a separate issue, I’d consider putting all of you own code into a module of its own, rather than in the ThisDocument module, which is really designed for quite specific porposes. Hope this gives some helpful ideas, but the subject of commandbars is quite a broad topic, so repost with any specific queries.
Alan
Kerry
I hadn’t realized that a custom toolbar was within your bounds of an acceptable solution. This is a better alternative to a button on the form for you, since there’s no issue with it printing. If this is the case, you can easily include any of your own macros on it. If your template is occassionally revised and redistributed, you’ll be better off “hard coding” the toolbar into the template, so that it is freshly created on the user’s system each time, as required – otherwise there can be problems with existing older versions.
It doesn’t appear that Leanne has used any of your macro code on the toolbar macros she included, and as said, you can include any of your own macros thereon. Is this (custom toolbar) the kind of solution you’re after?
Alan
Leanne you are a genius!
Is it also possible to make a button on the toolbar to protect and unprotect, since as well as spell check, they will want to unprotect a document and then reprotect it as a separate process.
Just so I am learning something here, does your macro call anything from the code I had in the “This Document” object? I am still trying to come to terms with the difference between modules etc. (There is tooooo much to learn)
Kerry
Kerry,
Is this something like you are looking for? I added a toolbar which stays with the template and corresponding document but the toolbar has 2 macros – one for Word 97 and one for Word 2000 and up (I couldn’t get the one for 2000 and up to work with 97). If you don’t have 2000 or higher, that icon can be pulled from the toolbar.
By selecting it, it unprotects the documents, spell checks form fields, then reprotects the document without losing any information.
(Edited by HansV to make URLs clickable – see Help 19)
Hi Alan
I have collected a few issues I really want to address in one form.
Firstly – users are unhappy with the loss of the spellchecking facility on Forms.
http://www.mvps.org/word/FAQs/MacrosVBA/Sp…kProtectDoc.htm%5B/url%5D
Secondly – when a form/template is unprotected and then reprotected they loose all information.
http://www.mvps.org/word/FAQs/MacrosVBA/Tu…FlfResetOff.htm%5B/url%5D
Thirdly – I want to tie all this together with a button on the form that will not show up when I print the document.
http://www.mvps.org/word/FAQs/TblsFldsFms/…PrintButton.htm%5B/url%5D
I never do things in small measures! Just a shame I know what I want to do, but rarely do I know how!
I have attached a form with the first two lots of code in, but I need major help in putting it all together. Once I get one form to work, I can use it for the basis of any further forms.
Any help is always greatly appreciated.
Kerry
I figured you wanted an “in-your-face” button, right in the document body, for the less savvy users. I tried some relatively simple code to set up a button (named btnProtect) operation, but have run into a problem I don’t understand. Maybe somebody more familiar with Word VBA will be able to identify the problem. Here is the code:
Private Sub Document_Open()
If ActiveDocument.ProtectionType wdNoProtection Then
btnProtect.Caption = “Unprotect”
Else
btnProtect.Caption = “Protect”
End If
End Sub
Private Sub btnProtect_Click()
Dim Doc As Document
Set Doc = ActiveDocument
With Doc
If .ProtectionType wdNoProtection Then
‘Doc is protected so remove protection
.Unprotect
btnProtect.Caption = “Protect”
Else
.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
‘ Runtime error 6461 – The ToolsProtectDocument statement is currently disabled.
btnProtect.Caption = “Unprotect”
End If
End With
End Sub
I’ve not looked at the printing issue and I think the events might be different for 97 and 2000. An easy way might be to exclude the printing of drawing objects – obviously not suitable if you do have some you need printing though. I’ll keep thinking about this though.
Alan
Thanks for the replies. I should have been more specific. My apologies to all.
I am making Forms for our workers who have a varying range of computer literacy. I am making forms with fields and protecting them simply to make it easy to tab through the document and fill in the fields. Sometimes a form needs to be unlocked for a few different reasons which I wont go into now. Unfortunately out of the hundreds of people that will use these forms, many dont know how to even find the Forms toolbar! It is easier to make the form do the work, than try and teach all of these people about Word. Hence my idea of putting command buttons on the form itself. I dont want it to be global.
I took a look at the workaround you mention and it looks good. I would appreciate assistance to do exactly as the example does, except not to Print, but to protect and unprotect the document.
Hope this makes things a little clearer.
Kerry
Hi Kerry
As Hans says, the functionality is already there, but do you need the code for your own custom incarnation? Rather than two buttons, you could use just one with a caption that toggles Protect/ Unprotect depending on the current value of the Protect property of the document.
As far a preventing a button from printing, I don’t think there’s a built-in property to control this – a bit of an oversight IMO if this is the case. There is a workaround here that might do the trick.
Alan
Kerry,
If you want to do it in VBA code, and want the buttons to be available in all documents, you can create a global template stored in the Word startup directory, containing the following two little Sub’s:
Public Sub ProtectionOff()
With ActiveDocument.
If .ProtectionType wdNo Protection Then
.Unprotect Password:=”password” ‘you can omit this if no password is required
End If
End With
End Sub
Public Sub ProtectionOn
With ActiveDocument
If .ProtectionType = wdNoProtection Then
.Protect Password:=”password” ‘add NoReset:=True, Type:=wdAllowOnlyFormFields if docs contain FormFields
End If
End With
End Sub
Create a toolbar with two buttons that call the above Subs.
Jan
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.