Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-25-2015, 12:17 PM
vss712 vss712 is offline Loop and Update Content Controls with value Windows 7 64bit Loop and Update Content Controls with value Office 2010 32bit
Novice
Loop and Update Content Controls with value
 
Join Date: Jun 2011
Posts: 5
vss712 is on a distinguished road
Default Loop and Update Content Controls with value

Hello,
I have several content controls with the same name within a document and in the headers and footers.


I'd like to loop through all controls with the name "AccountName" and update the content with a specific value.
Not certain how to do this for more than one content control, and also, it looks as though it is different in footers, headers and the main document.
can you please advise ?
Thanks
Reply With Quote
  #2  
Old 08-25-2015, 07:28 PM
Guessed's Avatar
Guessed Guessed is offline Loop and Update Content Controls with value Windows 7 32bit Loop and Update Content Controls with value Office 2010 32bit
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
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

A loop would not be required if you linked them all to the same xml node. However, the code to loop would look something like this
Code:
Sub CCLoader()
  Dim aCC As ContentControl
  For Each aCC In ActiveDocument.ContentControls
    If aCC.Title = "AccountName" Then
      aCC.Range.Text = "hi mum"
    End If
  Next
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 08-26-2015, 12:20 AM
gmayor's Avatar
gmayor gmayor is offline Loop and Update Content Controls with value Windows 7 64bit Loop and Update Content Controls with value Office 2010 32bit
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

For linking them see http://gregmaxey.com/word_tip_pages/...ntent_Controls
__________________
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-26-2015, 10:03 AM
vss712 vss712 is offline Loop and Update Content Controls with value Windows 7 64bit Loop and Update Content Controls with value Office 2010 32bit
Novice
Loop and Update Content Controls with value
 
Join Date: Jun 2011
Posts: 5
vss712 is on a distinguished road
Default

Thank you, Andrew. That works for the main Document, but doesn't work in Headers and Footers : (
Reply With Quote
  #5  
Old 08-26-2015, 04:05 PM
Guessed's Avatar
Guessed Guessed is offline Loop and Update Content Controls with value Windows 7 32bit Loop and Update Content Controls with value Office 2010 32bit
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
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 In that case

Try
Code:
Sub CCLoader()
  Dim aCC As ContentControl, aRng As Range
  For Each aRng In ActiveDocument.StoryRanges
    For Each aCC In aRng.ContentControls
      If aCC.Title = "aaa" Then
        aCC.Range.Text = "hi mum"
      End If
    Next aCC
  Next aRng
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #6  
Old 08-27-2015, 08:12 AM
vss712 vss712 is offline Loop and Update Content Controls with value Windows 7 64bit Loop and Update Content Controls with value Office 2010 32bit
Novice
Loop and Update Content Controls with value
 
Join Date: Jun 2011
Posts: 5
vss712 is on a distinguished road
Default

Thank you, Andrew - works everywhere except the header.... I haven't tried the footer though.
How is the header differentiated here ?
How do we access it for updating ?
Thanks!
Reply With Quote
  #7  
Old 08-27-2015, 08:27 AM
vss712 vss712 is offline Loop and Update Content Controls with value Windows 7 64bit Loop and Update Content Controls with value Office 2010 32bit
Novice
Loop and Update Content Controls with value
 
Join Date: Jun 2011
Posts: 5
vss712 is on a distinguished road
Default

PS -
Each of the controls for "AccountName" are bounding boxes. I ensure they are unlocked:

Still no luck with anything in the Page Headers. I do have Initial Page Header set to none, so the page header resides in another section, if that makes a difference.

Sub CCLoaderHiMum()
Dim aCC As ContentControl, aRng As Range

For Each aRng In ActiveDocument.StoryRanges


For Each aCC In aRng.ContentControls

If aCC.Title = "AccountName" Then
aCC.LockContents = False
aCC.Range.Text = "hi mum"
aCC.Range.Font.ColorIndex = wdBlack
aCC.LockContents = True
End If

Next aCC

Next aRng
End Sub
Reply With Quote
  #8  
Old 08-27-2015, 08:56 PM
Guessed's Avatar
Guessed Guessed is offline Loop and Update Content Controls with value Windows 7 32bit Loop and Update Content Controls with value Office 2010 32bit
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
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

If the second code sample I provided doesn't catch the CC in a header then I would guess that those CCs may be sitting inside a text box. We would need to see your document to work it out. Post a sample document if you want to resolve this.

Alternatively, explore the earlier suggestion of linking the CCs to an xml node and then change the value of that node.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop and Update Content Controls with value VBA to set Content controls as non printing Sammie0Sue Word VBA 21 01-12-2021 04:44 PM
VBA for content controls ciresuark Word VBA 1 03-10-2015 03:14 PM
Calculate Age From Content Controls kintap Word VBA 10 07-02-2014 09:25 AM
Windows update loop franklekens Windows 0 04-10-2014 11:56 PM
Loop and Update Content Controls with value Content Controls Sammie0Sue Word 6 11-06-2013 10:56 PM

Other Forums: Access Forums

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