Link to home
Start Free TrialLog in
Avatar of Tanya K
Tanya K

asked on

How to write a macro to protect fields within word

Hello.  I have created a form in word 2010 with four sections, each section to be completed by a seperate operator.  
Each section of the form has a combination of rich text content control fields, date pickers, and drop down lists.  
I would like to create a macro, assigned to a button under each section, such that once section one has been completed, the operator clicks the button and all the fields within section one are locked/protected from further editing.  The next macro assigned to a second button, would protect all the fields within section two.
I am aware that you can restrict editing, however the above is to make it easier for operators.
All and any assistance would be greatly appreciated.  
Thank you
Tanya
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tanya K
Tanya K

ASKER

Graham, thank you very much for your extremely prompt reply!  I have been able to apply your above code to my form for all my sections and it has worked perfectly.  I have been trying for the last couple of days to resolve this issue, and so I really appreciate your expertise.
 Many thanks, Tanya
Better, to reduce the amount of code,  you can use one procedure. Place the  each MacroButton field within the section and find the Section from the Selection.

Sub ProtectFields()
    Dim sec As Section
    Dim cc As ContentControl
    
    Set sec = Selection.Sections(1)
    For Each cc In sec.Range.ContentControls
        cc.LockContents = True
    Next cc
End Sub

Open in new window

Cross-posted, so I hadn't seen your prior comment