PDA

View Full Version : [SOLVED:] finding the name of a content control



MINCUS1308
10-02-2014, 11:35 AM
12344

I have a 2010 macro enabled word doc that has a table.
In this table there are various controls.
In writing the code that dictates the behaviors of my checkboxes I think I need to reference them by name.
My problem is I dont know how to adjust or even find the name property of a control in word.

does anyone have a good link to explain?
or can anyone get me started?

gmaxey
10-02-2014, 11:51 AM
Content Controls are "Named" and "Tagged" using the Developer>Controls>Properties dialog.

Since unlike formfields, you can have multiple CCs with a like title, tab or both, when referring to them in code you have to use the title or tag and an index number. For example if you have a single CC titled "Client Name" and tagged "XYZ" you could refer to it in code as:

ActiveDocument.SelectContentControlsByTitle("Name").Item(1)

or

ActiveDocument.SelectContentControlsbyTag("XYX").Item(1)

when a CC is created is gets an unique ID. You can also use the ID in code:


Sub ScratchMacroI()
'A basic Word macro coded by Greg Maxey
'To get a CCs unique ID, select is tab and run this macro:
MsgBox Selection.Range.ContentControls(1).ID
Debug.Print Selection.Range.ContentControls(1).ID
End Sub
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey.
'Target a CC by its unique ID.
ActiveDocument.ContentControls("294724017").Range.Text = "Some text"
End Sub


See: http://gregmaxey.mvps.org/word_tip_pages/content_controls.html

MINCUS1308
10-02-2014, 12:07 PM
so to reference any individual control in a word document I must call it by title and then its index number.
I can set the title of a control by using the Developer>Controls>Properties dialog.
But to find out the index number i either use trial by error or the code that you posted.

Did I understand correctly?

That seems awfully convoluted lol.
I should just stick to excel. jk, i've got to learn this eventually.

Thank you for you assistance gmaxey

gmaxey
10-02-2014, 12:44 PM
Well there are lots of ways to skin the cat. If you assign unique titles (or tags) to your CCs then it is fairly easy, but yes you do have to using the .Item() with either SelectByTitle or SelectbyTag. If you have three CCs titled ClientName you could tag them "ClientName1", "ClientName2" and "ClientName3" and then use

ActiveDocument.SelectionContentControlsbyTag("ClientName1").Item(1).range.text = "Moe"
ActiveDocument.SelectionContentControlsbyTag("ClientName2").Item(1).range.text = "Larry"
ActiveDocument.SelectionContentControlsbyTag("ClientName3").Item(1).range.text = "Curly"

macropod
10-02-2014, 11:14 PM
You could, of course, have a bunch of controls with the same title & different tags, or vice-versa, and loop through them with code like:

Dim i As Long
With ActiveDocument.SelectContentControlsByTitle("Title1")
For i = 1 To .Count
With .Item(I)
If .Tag = "Tag1" Then
.Range.Text = "Hello World"
Exit For
End If
End With
Next
End With
or:

Dim i As Long
With ActiveDocument.SelectContentControlsByTag("Tag1")
For i = 1 To .Count
With .Item(i)
If .Title = "Title1" Then
.Range.Text = "Hello World"
Exit For
End If
End With
Next
End With