Re: Populating Content Controls with VBA


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

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
Word MVP web site

KenderFaith wrote:
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 :-)