Populating Content Controls with VBA

K

KenderFaith

Something that really seems to be missing with content controls is the
ability to easily dump info into them programmatically, because of the
necessity to refer to them numerically, rather than by a unique value (like
Title). Is there a simple way to use SelectContentControlsByName (or Tag) to
identify the content control in which you wish to change the text value? The
big issue with referencing content controls by number is that if one content
control in a document is deleted, it effectively re-numbers the rest and
causes all kinds of issues...

Thanks :)
 
G

Greg Maxey

KenderFaith,

It is there, you just haven't found it yet ;-)

You can work with CCs by title, but since the title isn't unique to a CC you
have to specify which one you want to work with. Say you have three CCs
with the title "Name" and you want to work with the third one:

Sub WorkWithACCbyTitle()
Dim oCC As ContentControl
Set oCC = ActiveDocument.SelectContentControlsByTitle("Name").Item(1)
oCC.Range.Text = "Your text here."
End Sub

Still that can cause problems if you delete CCs titled "Name."

IMHO the most robust way to work with a CC is with its unique ID. This ID
remains with the CC throughout its life in the document (at least that is my
experience).

To find the unique ID, select the CC (click on it's tab) and run this code:

Sub GetUniqueID()
MsgBox Selection.ContentControls(1).ID
End Sub

Then work with the CC by ID like this:

Sub WorkWithACCbyID()
Dim oCC As ContentControl
Set oCC = ActiveDocument.ContentControls("#########") 'Replace # with actual
number values
oCC.Range.Text = "Your text there."
End Sub


--
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org
Word MVP web site http://word.mvps.org~~~~~~~~~~~~~~~~~~~~~~~~~~
 
K

KenderFaith

Wow - you ARE a Rock Star, Greg!

Thanks for the missing link - the light has come on & is so bright, I'm
looking for my shades...

:) Thanks a bunch!


Greg Maxey said:
Sorry, to work with the third one you would use:

Set oCC = ActiveDocument.SelectContentControlsByTitle("Name").Item(3)

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org
Word MVP web site http://word.mvps.org~~~~~~~~~~~~~~~... "Your text there." End Sub [/QUOTE] [/QUOTE]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top