Content Control ID

G

Greg Maxey

The Word Developer's Reference states that each content control has its own
unique ID so that you can read from it and write to it.

It is true, each has its own ID. You can get the ID of a selected CC using
this simple code:

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

What I can't figure out is a direct method of referring to a CC by its
unique ID in code. For example if I have a collection of CCs and 1 of them
has the ID 123456. How would I write a statement like:

Sub Test()
Dim myCC as ContentControl
Set myCC = ActiveDocument.ContentControls (?????)
With myCC
'Do something
End With
End Sub

If I give my CC as unique "Title" or "Tag" then I can use the
SelectContentControlbyTitle or SelectContentControlbyTag like this:

Sub Test()
Dim myCC as ContentControl
Set myCC = ActiveDocument.SelectContentControlsByTitle("MyTitle").Item(1)
With myCC
'Do something
End With
End Sub


but there isn't a SelectContentControlbyID :-(

It seems that since titles and tags are neccessarily unique to a CC then
there should be a direct method of referring to a CC by it ID like methods
for Title and Tag. All I have been able to come up with so far is looping
through the collection like this:

Sub Test()
Dim oCC1 As ContentControl
Dim myCC As ContentControl
For Each oCC1 In ActiveDocument.ContentControls
If oCC1.ID = 123456 Then
Set myCC = oCC1
Exit For
End If
Next
With myCC
'Do something
End With
End Sub

Am I missing something?
 
G

Greg Maxey

This

"It seems that since titles and tags are neccessarily unique ..."

Shoud be:

It seems that since titles and tags are "not" neccessarily unique ..."
 
G

Greg Maxey

Tristan Davis at Microsoft cleared this up for me.

It is:

ActiveDocument.ContentControls("123456")
 

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