#1
|
|||
|
|||
Lock All Content Controls, Except the First Instance of Each Title
I have a template with multiple different content controls that repeat throughout the document.
I want the end user to only be able to edit the first instance of each content control. This makes it incredibly difficult if I need to make changes to the document later on. Currently, I have a code that loops through all of them and unlocks the ability to edit each one, which makes it easy for me to make changes to the template. I then apply a code to go back through and lock all of them at once. I then manually unlock the first instance of each field, and publish the form. I am looking for code to automatically loop through the content controls, unlock the first instance of each title, and lock the rest. I found code that will do this for one specific title, but I have to repeat the code for each title. This is not preferred as there are several titles, which I change from time to time based on the needs of the template. |
#2
|
||||
|
||||
It seems to me you could manage any number of such content controls via code like:
Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean) Application.ScreenUpdating = False Dim i As Long With ActiveDocument.SelectContentControlsByTitle(CCtrl.Title) For i = 2 To .Count With .Item(i) .LockContents = False .Range.Text = CCtrl.Range.Text .LockContents = True End With Next End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
||||
|
||||
From the description it seems that you are duplicating content controls to repeat values throughout the document. That being the case, why don't you use mapped controls to repeat the data? Then whatever is entered in one of them is repeated in the others so that the need to lock and unlock and thus the need for a macro seems moot.
https://www.gmayor.com/insert_content_control_addin.htm makes it even simpler to insert mapped content controls. Insert the first one then copy and paste it to the other locations.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#4
|
|||
|
|||
My content controls are already mapped as such. I'm looking for code to change the "Contents cannot be edited" property for only specific instances of each mapped content control.
I'm using the following codes to lock or unlock all of the fields at once, but then I have to go back and manually unlock only the fields I want to be editable by the end user. None of this really applies to the published template, it's just to make it simpler for me to make changes on the back end without so much repetition. What I can't figure out is how to modify the code so it only applies to a specific instance of a given Title or Tag. Code:
Sub UnlockAllFormFields() ' Remove editing restrictions from all content controls Application.ScreenUpdating = False Dim objCC As ContentControl ' Loop through and select all content controls For Each objCC In ActiveDocument.ContentControls ' Un-check "Contents cannot be edited" box objCC.LockContents = False Next Application.ScreenUpdating = True End Sub Sub LockAllFormFields() ' Remove editing restrictions from all content controls Application.ScreenUpdating = False Dim objCC As ContentControl ' Loop through and select all content controls For Each objCC In ActiveDocument.ContentControls ' Un-check "Contents cannot be edited" box objCC.LockContents = True Next Application.ScreenUpdating = True End Sub Quote:
|
#5
|
|||
|
|||
Unfortunately, I can't seem to get this code to do anything.
Quote:
|
#6
|
|||
|
|||
Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 9/27/2018 Dim oCC As ContentControl Dim oCol As New Collection For Each oCC In ActiveDocument.ContentControls oCC.LockContents = False Next oCC On Error Resume Next For Each oCC In ActiveDocument.ContentControls oCol.Add oCC.Title, oCC.Title If Err.Number = 0 Then oCC.LockContents = True Err.Clear Next oCC lbl_Exit: Exit Sub End Sub |
#7
|
|||
|
|||
I reversed the "True/False" parts and now this does exactly what I was looking for. Thank you!
Quote:
|
#8
|
|||
|
|||
Oh! Sorry about that. I misread your earlier code.
You're welcome. |
#9
|
||||
|
||||
Greg's code is very elegant with the use of a Collection to determine if the code has already hit one of those CCs but it does rely on your consistent usage of the Title property for every CC.
An alternative to consider would be to just lock the CCs which are mapped to the same xpath as already used. There is also no real need to do two passes... Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 9/27/2018 Dim oCC As ContentControl, oCol As New Collection On Error Resume Next For Each oCC In ActiveDocument.ContentControls If aCC.XMLMapping.IsMapped then oCol.Add oCC.XMLMapping.XPath, oCC.XMLMapping.XPath oCC.LockContents = Not Err.Number = 0 Err.Clear Next oCC lbl_Exit: Exit Sub End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#10
|
|||
|
|||
Andrew, good follow on ;-).
Still, I am sort of in Graham's camp. I don't see the point of locking CCs which are mapped. You change one, they all change. Why go all the way back to the top of a document to change a value (in the first control) if you only realize it is wrong when you reach the bottom (in the last control)? |
#11
|
||||
|
||||
I also agree with Graham - there is no real point in locking the CCs. It will be very annoying having to go back up the file to FIND the first instance so I can just change it there.
If the user has the ability to change one then unlock them all.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
Tags |
content control, instance, title |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Word VBA : Lock the content of a table cell | User75 | Word Tables | 3 | 06-26-2018 05:37 AM |
Macro to save docx to doc that checks compatibility and converts content controls to static content. | staicumihai | Word VBA | 4 | 10-12-2016 08:23 PM |
Lock top row, so column title is always visible | Susanma | Excel | 1 | 03-30-2015 01:50 AM |
VBA for content controls | ciresuark | Word VBA | 1 | 03-10-2015 03:14 PM |
Creating a plain text content control for every instance of a word or phrase | RobsterCraw | Word VBA | 16 | 11-20-2012 03:25 PM |