Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-23-2018, 02:44 PM
therexjones therexjones is offline Lock All Content Controls, Except the First Instance of Each Title Windows 10 Lock All Content Controls, Except the First Instance of Each Title Office 2016
Novice
Lock All Content Controls, Except the First Instance of Each Title
 
Join Date: Sep 2018
Posts: 6
therexjones is on a distinguished road
Default 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.
Reply With Quote
  #2  
Old 09-23-2018, 03:35 PM
macropod's Avatar
macropod macropod is offline Lock All Content Controls, Except the First Instance of Each Title Windows 7 64bit Lock All Content Controls, Except the First Instance of Each Title Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 09-23-2018, 07:45 PM
gmayor's Avatar
gmayor gmayor is offline Lock All Content Controls, Except the First Instance of Each Title Windows 10 Lock All Content Controls, Except the First Instance of Each Title Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #4  
Old 09-27-2018, 09:25 AM
therexjones therexjones is offline Lock All Content Controls, Except the First Instance of Each Title Windows 10 Lock All Content Controls, Except the First Instance of Each Title Office 2016
Novice
Lock All Content Controls, Except the First Instance of Each Title
 
Join Date: Sep 2018
Posts: 6
therexjones is on a distinguished road
Default

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:
Originally Posted by gmayor View Post
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.
Reply With Quote
  #5  
Old 09-27-2018, 09:28 AM
therexjones therexjones is offline Lock All Content Controls, Except the First Instance of Each Title Windows 10 Lock All Content Controls, Except the First Instance of Each Title Office 2016
Novice
Lock All Content Controls, Except the First Instance of Each Title
 
Join Date: Sep 2018
Posts: 6
therexjones is on a distinguished road
Default

Unfortunately, I can't seem to get this code to do anything.

Quote:
Originally Posted by macropod View Post
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
Reply With Quote
  #6  
Old 09-27-2018, 10:03 AM
gmaxey gmaxey is offline Lock All Content Controls, Except the First Instance of Each Title Windows 7 32bit Lock All Content Controls, Except the First Instance of Each Title Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,441
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #7  
Old 09-27-2018, 11:05 AM
therexjones therexjones is offline Lock All Content Controls, Except the First Instance of Each Title Windows 10 Lock All Content Controls, Except the First Instance of Each Title Office 2016
Novice
Lock All Content Controls, Except the First Instance of Each Title
 
Join Date: Sep 2018
Posts: 6
therexjones is on a distinguished road
Default

I reversed the "True/False" parts and now this does exactly what I was looking for. Thank you!

Quote:
Originally Posted by gmaxey View Post
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
Reply With Quote
  #8  
Old 09-27-2018, 04:56 PM
gmaxey gmaxey is offline Lock All Content Controls, Except the First Instance of Each Title Windows 7 32bit Lock All Content Controls, Except the First Instance of Each Title Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,441
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Oh! Sorry about that. I misread your earlier code.

You're welcome.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #9  
Old 09-27-2018, 09:33 PM
Guessed's Avatar
Guessed Guessed is online now Lock All Content Controls, Except the First Instance of Each Title Windows 10 Lock All Content Controls, Except the First Instance of Each Title Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,001
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #10  
Old 09-28-2018, 04:23 AM
gmaxey gmaxey is offline Lock All Content Controls, Except the First Instance of Each Title Windows 7 32bit Lock All Content Controls, Except the First Instance of Each Title Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,441
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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)?
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #11  
Old 09-28-2018, 06:38 AM
Guessed's Avatar
Guessed Guessed is online now Lock All Content Controls, Except the First Instance of Each Title Windows 10 Lock All Content Controls, Except the First Instance of Each Title Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,001
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
Reply

Tags
content control, instance, title



Similar Threads
Thread Thread Starter Forum Replies Last Post
Lock All Content Controls, Except the First Instance of Each Title 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 All Content Controls, Except the First Instance of Each Title 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
Lock All Content Controls, Except the First Instance of Each Title Creating a plain text content control for every instance of a word or phrase RobsterCraw Word VBA 16 11-20-2012 03:25 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:53 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft