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
    1. Advertisements

  2. Igor

    Greg Maxey Guest

    Igor,

    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,
    etc.)

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

    or the same thing by tag:

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

    Personally I think working with the unique ID is the more robust method.



    Igor wrote:
    > If I have a Text content control on a word document, how would I find
    > this exact control and manipulate it?


    --
    Greg Maxey - Word MVP

    My web site http://gregmaxey.mvps.org
     
    Greg Maxey, Jun 23, 2009
    #2
    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
    exception.

    "Greg Maxey" wrote:

    > Igor,
    >
    > 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,
    > etc.)
    >
    > Sub ManipulateCCsByTitle()
    > ActiveDocument.SelectContentControlsByTitle("Name").Item(1).Range.Text =
    > "Igor"
    > End Sub
    >
    > or the same thing by tag:
    >
    > Sub ManipulateCCsby Tag
    > ActiveDocument.SelectContentControlsByTag("SalesDate").Item(1).Range.Text =
    > "6/22/2009"
    > End Sub
    >
    > Personally I think working with the unique ID is the more robust method.
    >
    >
    >
    > Igor wrote:
    > > If I have a Text content control on a word document, how would I find
    > > this exact control and manipulate it?

    >
    > --
    > Greg Maxey - Word MVP
    >
    > My web site http://gregmaxey.mvps.org
    >
    >
    >
    >
    >
     
    Igor, Jun 23, 2009
    #3
  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" wrote:

    > 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
    > exception.
    >
    > "Greg Maxey" wrote:
    >
    > > Igor,
    > >
    > > 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,
    > > etc.)
    > >
    > > Sub ManipulateCCsByTitle()
    > > ActiveDocument.SelectContentControlsByTitle("Name").Item(1).Range.Text =
    > > "Igor"
    > > End Sub
    > >
    > > or the same thing by tag:
    > >
    > > Sub ManipulateCCsby Tag
    > > ActiveDocument.SelectContentControlsByTag("SalesDate").Item(1).Range.Text =
    > > "6/22/2009"
    > > End Sub
    > >
    > > Personally I think working with the unique ID is the more robust method.
    > >
    > >
    > >
    > > Igor wrote:
    > > > If I have a Text content control on a word document, how would I find
    > > > this exact control and manipulate it?

    > >
    > > --
    > > Greg Maxey - Word MVP
    > >
    > > My web site http://gregmaxey.mvps.org
    > >
    > >
    > >
    > >
    > >
     
    Igor, Jun 23, 2009
    #4
  5. Igor

    Guest

    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
    #5
    1. Advertisements

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.
Similar Threads
  1. Troy Zeleznik

    Child Documents and Embeded controls

    Troy Zeleznik, Jul 28, 2003, in forum: Word VBA
    Replies:
    1
    Views:
    74
    Word Heretic
    Aug 1, 2003
  2. Jens Wallman
    Replies:
    2
    Views:
    168
    Jens Wallman
    Oct 9, 2003
  3. sai
    Replies:
    1
    Views:
    87
    Word Heretic
    Nov 9, 2003
  4. Joshua

    Generic vs. Built-in Controls

    Joshua, Nov 2, 2003, in forum: Word VBA
    Replies:
    0
    Views:
    102
    Joshua
    Nov 2, 2003
  5. vera

    ActiveX Controls on Documents

    vera, Jan 9, 2004, in forum: Word VBA
    Replies:
    2
    Views:
    90
    Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT
    Jan 9, 2004
  6. Martin Newman

    Working with controls in a word document

    Martin Newman, Feb 14, 2004, in forum: Word VBA
    Replies:
    5
    Views:
    154
    Martin Newman
    Feb 14, 2004
  7. Koen
    Replies:
    1
    Views:
    81
    Cindy M.
    Sep 14, 2006
  8. sarahj
    Replies:
    3
    Views:
    376
    Greg Maxey
    Apr 24, 2009
Loading...