0
votes

I want to disable this Restrict Style Changes in protected Microsoft Word documents with VBA Code.

https://helpdeskgeek.com/office-tips/restrict-editing-on-word-documents/

This code doesn't work for me:

Sub DisableCheckBox()
    ActiveDocument.Unprotect
    ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True, enforcestylelock:=False
End Sub

Any ideas?

Thanks very much.

Edit:

OK. I try to explain my problem.

I have many documents. They have Text and Formular Fields to fill with variable short text. The documents are protected to fill only formulars fields and additionally - and this is the problem - there is activate “limit formating to a selection of styles.”

Complete Text with formular fields is formated to Arial 10 pt. Some formular fields are Arial 12 pt.

When user fill text in the protect document the text is Verdana 12 pt, because this is the default style.That’s the reason I want to deactivate the option “limit formating to a selection of styles.”

Next step, I try in my vba code in a new word document:

https://docs.microsoft.com/en-us/office/vba/api/word.document.protect

ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True, enforcestylelock:=False Result: “limit formating to a selection of styles” not activated

Then I try in another new document:

ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True, enforcestylelock:=True Result: “limit formating to a selection of styles” activated

The code works for new documents (checkbox “limit formating to a selection of styles” is on or off).

Now I try the code for my existing protected documents with activated option “limit formating to a selection of styles.”

ActiveDocument.Unprotect

ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True, enforcestylelock:=False

Result: “limit formating to a selection of styles” is NOT deactivated

I don’t know why?

It is only necessary for me to disable the checkbox option “limit formating to a selection of styles.” with VBA (you can see the checkbox in the picture).

Thank you.

enter image description here

1
"The code doesn't work for me" doesn't tell us anything about what problem you're having. Please describe the symptoms. Since you're protecting for forms, style application is not possible in the protected areas. You should still be able apply styles in unprotected sections.John Korchok

1 Answers

1
votes

There seems to be a bug with removing the Style restrictions. You could work around that via code like:

Sub DisableCheckBox()
Dim Stl As Style
With ActiveDocument
  On Error Resume Next
  .Unprotect
  On Error GoTo 0
  For Each Stl In .Styles
    Stl.Locked = False
  Next
  .Protect NoReset:=True, Type:=wdAllowOnlyFormFields
End With
End Sub

Do note that unless you capture and store the names of the previously allowed or disallowed Styles, the process can't be undone in code later on.