Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    4 Star Lounger
    Join Date
    Apr 2002
    Location
    Northern Territory, Australia
    Posts
    471
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Command Button to Unprotect Document (Word 97)

    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

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Command Button to Unprotect Document (Word 97)

    The Forms toolbar already has a button to do this, so you don't really need command buttons on the document itself.
    Attached Images Attached Images
    • File Type: png x.PNG (1.9 KB, 6 views)

  3. #3
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: 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

  4. #4
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Command Button to Unprotect Document (Word 97)

    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

  5. #5
    4 Star Lounger
    Join Date
    Apr 2002
    Location
    Northern Territory, Australia
    Posts
    471
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Re: Command Button to Unprotect Document (Word 97)

    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

  6. #6
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Command Button to Unprotect Document (Word 97)

    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
    <font color=red>' Runtime error 6461 - The ToolsProtectDocument statement is currently disabled.</font color=red>
    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

  7. #7
    2 Star Lounger
    Join Date
    Jul 2001
    Location
    Wisconsin, USA
    Posts
    193
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Command Button to Unprotect Document (Word 97)

    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.
    Attached Files Attached Files

  8. #8
    4 Star Lounger
    Join Date
    Apr 2002
    Location
    Northern Territory, Australia
    Posts
    471
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Re: Command Button to Unprotect Document (Word 97)

    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

  9. #9
    4 Star Lounger
    Join Date
    Apr 2002
    Location
    Northern Territory, Australia
    Posts
    471
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Re: Command Button to Unprotect Document (Word 97)

    (Edited by HansV to make URLs clickable - see <!help=19>Help 19<!/help>)

    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

    Secondly - when a form/template is unprotected and then reprotected they loose all information.

    http://www.mvps.org/word/FAQs/MacrosVBA/Tu...FlfResetOff.htm

    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

    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
    Attached Files Attached Files

  10. #10
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Command Button to Unprotect Document (Word 97)

    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

  11. #11
    4 Star Lounger
    Join Date
    Apr 2002
    Location
    Northern Territory, Australia
    Posts
    471
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Re: Command Button to Unprotect Document (Word 97)

    Hi Alan

    Yes this is turning out to be a good solution to my problem. It solves my printing issues well.

    I am not sure what you mean by "hard coding" the toolbar to the template? Can you elaborate a bit more on what this means?

    Kerry

  12. #12
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Command Button to Unprotect Document (Word 97)

    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 <img src=/S/grin.gif border=0 alt=grin width=15 height=15> ) 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
    Attached Files Attached Files

  13. #13
    4 Star Lounger
    Join Date
    Apr 2002
    Location
    Northern Territory, Australia
    Posts
    471
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Custom Command Bar

    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
    Attached Files Attached Files

  14. #14
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Custom Command Bar

    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>post 329047</post#>, 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. <img src=/S/grin.gif border=0 alt=grin width=15 height=15> 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).

  15. #15
    4 Star Lounger
    Join Date
    Apr 2002
    Location
    Northern Territory, Australia
    Posts
    471
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Re: Custom Command Bar

    Alan your edit is correct - that is what I was looking to do, but I am confused as to where the new lines of code would go in the module.?

    Kerry

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •