Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-28-2017, 07:53 AM
sylvio sylvio is offline Delete or merge Content controls with the same name Windows 7 64bit Delete or merge Content controls with the same name Office 2010 32bit
Novice
Delete or merge Content controls with the same name
 
Join Date: Jan 2017
Posts: 20
sylvio is on a distinguished road
Default Delete or merge Content controls with the same name

Can you please suggest how to check whether CCs with the same name have the same values?


If yes, delete one of the CCs, if different values - merge values in one CC.
Reply With Quote
  #2  
Old 08-28-2017, 03:55 PM
macropod's Avatar
macropod macropod is offline Delete or merge Content controls with the same name Windows 7 64bit Delete or merge Content controls with the same name 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

You cannot merge content controls - you'd have to move the content from one to the other, then delete the one you move the content from. The approach you'd take for this depends in part on whether the content controls have the same title, or different titles.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 08-28-2017, 10:37 PM
gmayor's Avatar
gmayor gmayor is offline Delete or merge Content controls with the same name Windows 10 Delete or merge Content controls with the same name Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
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

Paul has identified the flaw in your requirement, however you may find http://www.gmayor.com/BookmarkandVariableEditor.htm (Content Control Editor Version) useful in identifying similarly named content controls and as an aid to changing their values, and/or deleting unwanted controls.

If you want controls to duplicate values in controls in different parts of the document, then the controls should be mapped and cross referenced. This will do that also.
__________________
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 08-29-2017, 04:02 AM
sylvio sylvio is offline Delete or merge Content controls with the same name Windows 7 64bit Delete or merge Content controls with the same name Office 2010 32bit
Novice
Delete or merge Content controls with the same name
 
Join Date: Jan 2017
Posts: 20
sylvio is on a distinguished road
Default

macropod, gmayor, thank you, I meant the same title.
As an example, two situations I am talking about:
1. Both CCs have title "A" and the same content "Apple". In this case the macro should delete one of the CCs.
2. Both CCs have title "A" but the content of one of the two is "Apple" while of the second is "Orange". In this case the both contents are merged as "Apple, Orange" and moved to one of the CCs, the second is deleted.
Is it possible?
Reply With Quote
  #5  
Old 08-29-2017, 04:39 AM
macropod's Avatar
macropod macropod is offline Delete or merge Content controls with the same name Windows 7 64bit Delete or merge Content controls with the same name 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

Try something based on:
Code:
Sub Demo()
Dim i As Long, StrTtl As String, StrTxt As String
StrTtl = "MyTitle": StrTxt = "|"
With ActiveDocument
  On Error Resume Next
  For i = .SelectContentControlsByTitle(StrTtl).Count To 2 Step -1
    With .SelectContentControlsByTitle(StrTtl)(i)
      If InStr(StrTxt, "|" & Trim(.Range.Text) & "|") > 0 Then
        .Range.Text = vbNullString
        .LockContentControl = False
        .Delete
      ElseIf .ShowingPlaceholderText = True Then
        .Delete
      Else
        StrTxt = "|" & Trim(.Range.Text) & StrTxt
        .Range.Text = vbNullString
        .LockContentControl = False
        .Delete
      End If
    End With
  Next
  With .SelectContentControlsByTitle(StrTtl)(1).Range
    StrTxt = Replace(StrTxt, "|" & Trim(.Text), "")
    StrTxt = Left(StrTxt, Len(StrTxt) - 1)
    StrTxt = Replace(StrTxt, "|", ", ")
    .Text = Trim(.Text) & StrTxt
  End With
End With
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 08-29-2017, 07:21 AM
sylvio sylvio is offline Delete or merge Content controls with the same name Windows 7 64bit Delete or merge Content controls with the same name Office 2010 32bit
Novice
Delete or merge Content controls with the same name
 
Join Date: Jan 2017
Posts: 20
sylvio is on a distinguished road
Default

Thank you, it works as expected for a single title of CCs.
When I try to adapt the code for an array of names (I need to check about 20 differently named CCs and their duplicates) using cycle For ... Next, the result is dissapointing.
For example, for two CCs named "A" with contents "1" and "2" and CCs named "B" with contents "3" and "4" the result is CCs "A" with content "12" and "B" with "3, 412". And it is expect "A" with "1, 2" and "B" with "3, 4".
What is wrong?

Here is the code I have used:

Code:
Sub Demo()
Dim i, j As Long, StrTtl As String, StrTxt As String, CCnames
CCnames = Array("A", "B"): StrTxt = "|"

For j = 0 To UBound(CCnames)
    With ActiveDocument
       On Error Resume Next
         For i = .SelectContentControlsByTitle(CCnames(j)).Count To 2 Step -1
        With .SelectContentControlsByTitle(CCnames(j))(i)
          If InStr(StrTxt, "|" & Trim(.Range.Text) & "|") > 0 Then
            .Range.Text = vbNullString
            .Delete
          ElseIf .ShowingPlaceholderText = True Then
            .Delete
          Else
            StrTxt = "|" & Trim(.Range.Text) & StrTxt
            .Range.Text = vbNullString
            .Delete
          End If
        End With
      Next
      
      With .SelectContentControlsByTitle(CCnames(j))(1).Range
        StrTxt = Replace(StrTxt, "|" & Trim(.Text), "")
        StrTxt = Left(StrTxt, Len(StrTxt))
        StrTxt = Replace(StrTxt, "|", ", ")
        .Text = Trim(.Text) & StrTxt
        
      End With
    
   End With
Next
Reply With Quote
  #7  
Old 08-29-2017, 02:29 PM
macropod's Avatar
macropod macropod is offline Delete or merge Content controls with the same name Windows 7 64bit Delete or merge Content controls with the same name 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

Quote:
Originally Posted by sylvio View Post
Thank you, it works as expected for a single title of CCs.
When I try to adapt the code for an array of names (I need to check about 20 differently named CCs and their duplicates) using cycle For ... Next, the result is dissapointing.
For example, for two CCs named "A" with contents "1" and "2" and CCs named "B" with contents "3" and "4" the result is CCs "A" with content "12" and "B" with "3, 412". And it is expect "A" with "1, 2" and "B" with "3, 4".
What is wrong?
What is wrong is that you're not resetting StrTxt to "|" on each iteration of the loop; you currently 'StrTxt = "|"' outside the loop.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #8  
Old 08-30-2017, 01:12 AM
sylvio sylvio is offline Delete or merge Content controls with the same name Windows 7 64bit Delete or merge Content controls with the same name Office 2010 32bit
Novice
Delete or merge Content controls with the same name
 
Join Date: Jan 2017
Posts: 20
sylvio is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
What is wrong is that you're not resetting StrTxt to "|" on each iteration of the loop
Many thanks!
Now the code works as expected


Code:
Sub Demo()
Dim i, j As Long, StrTtl As String, StrTxt As String, CCnames
CCnames = Array("A", "B", "C"): StrTxt = "|"

For j = 0 To UBound(CCnames)
    With ActiveDocument
         On Error Resume Next
          For i = .SelectContentControlsByTitle(CCnames(j)).Count To 2 Step -1
            With .SelectContentControlsByTitle(CCnames(j))(i)
              If InStr(StrTxt, "|" & Trim(.Range.Text) & "|") > 0 Then
                  .Range.Text = vbNullString
                  .Delete
                ElseIf .ShowingPlaceholderText = True Then
                  .Delete
                Else
                  StrTxt = "|" & Trim(.Range.Text) & StrTxt
                  .Range.Text = vbNullString
                  .Delete
              End If
             End With
           Next
        
        With .SelectContentControlsByTitle(CCnames(j))(1).Range
          StrTxt = Replace(StrTxt, "|" & Trim(.Text), "")
          StrTxt = Left(StrTxt, Len(StrTxt) - 1)
          StrTxt = Replace(StrTxt, "|", ", ")
          .Text = Trim(.Text) & StrTxt
          StrTxt = "|"
        End With
    
    End With
Next
End Sub
Reply With Quote
  #9  
Old 08-30-2017, 01:18 AM
macropod's Avatar
macropod macropod is offline Delete or merge Content controls with the same name Windows 7 64bit Delete or merge Content controls with the same name 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

All you needed to do was move 'StrTxt = "|"' to follow the line:
For j = 0 To UBound(CCnames)
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #10  
Old 08-30-2017, 01:46 AM
sylvio sylvio is offline Delete or merge Content controls with the same name Windows 7 64bit Delete or merge Content controls with the same name Office 2010 32bit
Novice
Delete or merge Content controls with the same name
 
Join Date: Jan 2017
Posts: 20
sylvio is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
All you needed to do was move 'StrTxt = "|"' to follow the line:
For j = 0 To UBound(CCnames)
Indeed, thank you!
Reply With Quote
Reply

Tags
content controls



Similar Threads
Thread Thread Starter Forum Replies Last Post
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
Delete or merge Content controls with the same name Bookmark content controls silverspr Word VBA 24 06-14-2016 05:19 AM
VBA for content controls ciresuark Word VBA 1 03-10-2015 03:14 PM
Delete or merge Content controls with the same name Content Controls Sammie0Sue Word 6 11-06-2013 10:56 PM
Delete or merge Content controls with the same name Grouping Content Controls cksm4 Word VBA 2 03-01-2011 12:46 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 08: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