none
How do you obtaining the values of content Controls in Word from a VB form using interop routines? RRS feed

  • Question

  • I have a word 2010 document which has a number of content controls that a person will fill in.

    I am looking to open the document and read the values entered from withing our VB forms application (VS2008).

    The application does alot with creating Word and Excel documents using the standard interop routines. But I canbot see how to access the content control fields on a Word form.

    Can anyone give me some pointers?

    Thanks.


    Don C

    Wednesday, February 13, 2013 11:11 PM

Answers

  • I think that I have discovered what I need. The following does return the value of the first content control field in the document.

    FieldValue1 = oDoc.contentcontrols(1).Range.text
    MessageBox.Show(FieldValue1)

    ----------

    Expanding to make the code more flexible I have found the following to be usefull appart from the fact that Word does not enforce unique "tags" or "titles".

    FieldValue1 = oDoc.selectcontentcontrolsbytag("TextField1").item(1).Range.text
    MessageBox.Show(FieldValue1)

    FieldValue1 = oDoc.selectcontentcontrolsbytitle("FirstNames").item(1).Range.text
    MessageBox.Show(FieldValue1)

    I have a few more things to learn about these beasts but its a good starting point.


    Don C

    Thursday, February 14, 2013 12:10 PM

All replies

  • Hi Donald,

    Thanks for posting in the MSDN Forum.

    I think you need to use "Application.ActiveDocument.ContentControls" to iterate the content controls to access the value.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, February 14, 2013 2:55 AM
    Moderator
  • I think that I have discovered what I need. The following does return the value of the first content control field in the document.

    FieldValue1 = oDoc.contentcontrols(1).Range.text
    MessageBox.Show(FieldValue1)

    ----------

    Expanding to make the code more flexible I have found the following to be usefull appart from the fact that Word does not enforce unique "tags" or "titles".

    FieldValue1 = oDoc.selectcontentcontrolsbytag("TextField1").item(1).Range.text
    MessageBox.Show(FieldValue1)

    FieldValue1 = oDoc.selectcontentcontrolsbytitle("FirstNames").item(1).Range.text
    MessageBox.Show(FieldValue1)

    I have a few more things to learn about these beasts but its a good starting point.


    Don C

    Thursday, February 14, 2013 12:10 PM