PDA

View Full Version : Content Control and VBA



austin350s10
11-18-2011, 08:48 AM
Does anyone know how to reference a Rich Text Content Control field using VBA. I am trying to write some VBA code that will look at the value in a Content Control field and run a script based on the field. Only problem is I cant figure out the reference the field using VBA.

gmaxey
11-18-2011, 12:10 PM
Sub ScratchMacro()
'CCs can be referenced in the following ways:
' 1. By their position (Index number) in the document.
' 2. By Title and item number
' 3. By Tag ane item number
' 4. By a unique ID defined when the CC is created.
MsgBox ActiveDocument.ContentControls(1).Range.Text
'Since Title and Tag are not unique you need to use Item as well.
MsgBox ActiveDocument.SelectContentControlsByTitle("Test").Item(1).Range.Text
MsgBox ActiveDocument.SelectContentControlsByTag("Test").Item(1).Range.Text
'Select the CC and get its ID
Debug.Print Selection.Range.ContentControls(1).ID
'Reference by ID.
MsgBox ActiveDocument.ContentControls("420146660").Range.Text
End Sub