none
VBA and referencing Content Controls RRS feed

  • Question

  • Hi, I'm just getting started with content controls in Word 2013 and I'm looking to replace Bookmarks with Content Controls.

    I can't seem to find how I can reference a content control if I want to set it. I can only see how to set it if you know what number control it is rather than ID.

    For example, I am currently setting a name control control field in this manner:

    Dim oCC as ContentControl

    Set oCC  = ActiveDocument.ContentControls(1)

    oCC.Range.Text = "Joe Bloggs"

    Obviously if there are a dozen controls then this can get messy. What I would prefer to do is something like:

    Dim oCC as ContentControl

    Set oCC  = ActiveDocument.ContentControls("txtPersonsName")

    oCC.Range.Text = "Joe Bloggs"

    Is this possible?

    Thanks

    Wednesday, October 1, 2014 4:33 PM

Answers

  • If you assign titles or tags to your content controls, you can address them by those, using code like:

    ActiveDocument.SelectContentControlsByTitle("TitleName").Item(1).Range.Text = "Joe Bloggs"

    or:

    ActiveDocument.SelectContentControlsByTag("TagName").Item(1).Range.Text = "Joe Bloggs"

    Note that these methods allow you to have more than one content control with the same title or tag.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by Dan Rooney Thursday, October 2, 2014 8:07 AM
    • Unmarked as answer by Dan Rooney Thursday, October 2, 2014 8:11 AM
    • Marked as answer by Dan Rooney Thursday, October 2, 2014 8:24 AM
    Thursday, October 2, 2014 1:44 AM

All replies

  • This forum supports VB.Net in Visual Studio. For questions about VBA in Word, try the Word for Developers Forum.
    Wednesday, October 1, 2014 4:47 PM
  • If you assign titles or tags to your content controls, you can address them by those, using code like:

    ActiveDocument.SelectContentControlsByTitle("TitleName").Item(1).Range.Text = "Joe Bloggs"

    or:

    ActiveDocument.SelectContentControlsByTag("TagName").Item(1).Range.Text = "Joe Bloggs"

    Note that these methods allow you to have more than one content control with the same title or tag.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by Dan Rooney Thursday, October 2, 2014 8:07 AM
    • Unmarked as answer by Dan Rooney Thursday, October 2, 2014 8:11 AM
    • Marked as answer by Dan Rooney Thursday, October 2, 2014 8:24 AM
    Thursday, October 2, 2014 1:44 AM
  • Thanks Paul, works perfectly!
    Thursday, October 2, 2014 8:25 AM