Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-28-2017, 07:39 AM
sylvio sylvio is offline Check if named Content Control field exists and add missing Windows 7 64bit Check if named Content Control field exists and add missing Office 2010 32bit
Novice
Check if named Content Control field exists and add missing
 
Join Date: Jan 2017
Posts: 20
sylvio is on a distinguished road
Default Check if named Content Control field exists and add missing

Hello,

I am trying to code a macro which could check whether a document has all needed named content controls and add missing if necessary.
Let's say, the document should contain 3 CCs named "A", "B" and "C". If it contains only "A", the macro should add "B" and "C".



My code is below. It adds all CCs if some of the 3 named CC are missing, but do not differentiate which one should be added. How to add check for missing CCs?

Code:
Sub AddMissingCCs()
Dim CCnames
Dim i As Long
Dim j As Long
Dim CCcount As Long
Dim CCtrl As ContentControl

CCnames = Array("A", "B", "C")

For i = 0 To UBound(CCnames)
    For Each CCtrl In ActiveDocument.ContentControls
      If (UBound(Filter(CCnames, CCtrl.Title)) = -1) Then
        Selection.EndKey Unit:=wdStory
        ActiveDocument.Range.InsertAfter vbCr
        Selection.Range.ContentControls.Add (wdContentControlPlain)
        Selection.ParentContentControl.Title = CCnames(i)
       End If
    Next
Next

End Sub
Reply With Quote
  #2  
Old 08-28-2017, 03:52 PM
macropod's Avatar
macropod macropod is offline Check if named Content Control field exists and add missing Windows 7 64bit Check if named Content Control field exists and add missing Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

Try:
Code:
Sub Demo()
Dim i As Long, ArrNames, CCtrl As ContentControl
ArrNames = Array("A", "B", "C")
With ActiveDocument
  For i = 0 To UBound(ArrNames)
    On Error Resume Next
    Set CCtrl = .SelectContentControlsByTitle(ArrNames(i))(1)
    On Error GoTo 0
    If CCtrl Is Nothing Then
      .Range.InsertAfter " "
      Set CCtrl = .ContentControls.Add(wdContentControlText, .Range.Characters.Last)
      CCtrl.Title = ArrNames(i)
    End If
    Set CCtrl = Nothing
  Next
End With
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 08-29-2017, 04:08 AM
sylvio sylvio is offline Check if named Content Control field exists and add missing Windows 7 64bit Check if named Content Control field exists and add missing Office 2010 32bit
Novice
Check if named Content Control field exists and add missing
 
Join Date: Jan 2017
Posts: 20
sylvio is on a distinguished road
Default

macropod, thank you very much!
It works as expected.
Reply With Quote
Reply

Tags
content controls



Similar Threads
Thread Thread Starter Forum Replies Last Post
Check if named Content Control field exists and add missing Show field shading in Content Control ajanson Word 3 08-15-2016 04:49 PM
Have Text Entered in Content Control / PreDefined Field Populate Field in Footer bzowk Word 1 04-14-2016 11:50 AM
Check Box Content Control when checked users are presented with an option cryder Word 0 01-07-2016 05:11 AM
Check if named Content Control field exists and add missing Replace checkbox symbol with check box content control canadansk Word VBA 5 04-01-2015 08:21 AM
Check if named Content Control field exists and add missing Adding auto text to end of content control field ksigcajun Word VBA 4 10-13-2014 05:37 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:33 AM.


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