Finding Controls

Discussion in 'Word VBA' started by Igor, Jun 23, 2009.

  1. Igor

    Igor Guest

    If I have a Text content control on a word document, how would I find this
    exact control and manipulate it?
    Igor, Jun 23, 2009
    1. Advertisements

  2. Igor

    Greg Maxey Guest


    Lets assume you have two text CCs. One is titled "Name" and tagged
    "CustomerName" the other is titled "Date" and tagged "SalesDate."

    There are several ways to work with and manipulate these controls.

    1. A ContentControl is part of an indexed collection of ContentControls.
    This means that you can work with them by index number:

    Sub ManipulateCCsByIndex()
    ActiveDocument.ContentControls(1).Range.Text = "Igor"
    ActiveDocument.ContentControls(2).Range.Text = "6/22/2009"
    End Sub

    When a CC is created Word assigns it a unique ID. You can obtain this ID by
    selecting the control and running this bit of code:

    Sub GetCCID()
    MsgBox Selection.Range.ContentControls(1).ID
    End Sub

    Sub ManipulateCCsByID()
    ActiveDocument.ContentControls("932358501").Range.Text = "Igor"
    ActiveDocument.ContentControls("932358504").Range.Text = "6/22/2009"
    End Sub

    Note: your unique ID number will be different than mine.

    You can work with CCs by title. Since you can have more that one CC with
    the same title you must include the item number (i.e., first, second, third,

    Sub ManipulateCCsByTitle()
    ActiveDocument.SelectContentControlsByTitle("Name").Item(1).Range.Text =
    End Sub

    or the same thing by tag:

    Sub ManipulateCCsby Tag
    ActiveDocument.SelectContentControlsByTag("SalesDate").Item(1).Range.Text =
    End Sub

    Personally I think working with the unique ID is the more robust method.
    Greg Maxey, Jun 23, 2009
    1. Advertisements

  3. Igor

    Igor Guest

    Thank you Greg for the response. What is a good way to first check if the
    control exists in the document first. If so, I would manipulate it. I can't
    assume that the control will be on the document, otherwise this will cause an
    Igor, Jun 23, 2009
  4. Igor

    Igor Guest

    I have looked at this and I think good to go with a foreach loop that loops
    through all content controls and if I find a match on the title, then
    manipulate the content control.
    Igor, Jun 23, 2009
  5. Igor


    I agree with Igor is that you need to set up a loop to figure out which CC you are in and which is the next one. THE big problem is that the Index ofan item is NOT the same as its Count.

    For example, I could have 3 CCs with a title and tag of KidName. I want tofind out which KidName I'm in and then move to the next KidName. From experimenting with them (by making the CC.Range.Item(i).Text = Str(i)) I seethat the Index seems to be in an almost random order (though 1 time I thought I saw it was in alphabetical order for different tags, but couldn't figure out the order for those with the same name.) I tried making a CC and copying and pasting it in to see if that set the order, but it didn't. Makes it VERY much a pain to work with.
    , Apr 19, 2014
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.