Discussion:
Populating Content Controls with VBA
(too old to reply)
KenderFaith
2008-02-14 20:17:04 UTC
Permalink
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 :-)
Greg Maxey
2008-02-14 20:36:49 UTC
Permalink
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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Post by 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 :-)
Greg Maxey
2008-02-14 20:41:29 UTC
Permalink
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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Post by 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
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).
Sub GetUniqueID()
MsgBox Selection.ContentControls(1).ID
End Sub
Sub WorkWithACCbyID()
Dim oCC As ContentControl
Set oCC = ActiveDocument.ContentControls("#########") 'Replace # with
actual number values
oCC.Range.Text = "Your text there."
End Sub
Post by 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 :-)
KenderFaith
2008-02-20 17:49:03 UTC
Permalink
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!
Post by Greg Maxey
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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Post by 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
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).
Sub GetUniqueID()
MsgBox Selection.ContentControls(1).ID
End Sub
Sub WorkWithACCbyID()
Dim oCC As ContentControl
Set oCC = ActiveDocument.ContentControls("#########") 'Replace # with
actual number values
oCC.Range.Text = "Your text there."
End Sub
Post by 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 :-)
Greg Maxey
2008-02-14 20:38:32 UTC
Permalink
You might also find this useful:

http://gregmaxey.mvps.org/Mapped_Content_Control.htm
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org
Word MVP web site http://word.mvps.org
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Post by 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 :-)
Loading...