none
How do I read a form field value in a Word 2010 macro? RRS feed

  • Question

  • I have a plain text content control (see http://screencast.com/t/0LVczDkSN for a screenshot) used to collect user input.  I have a sub routine that needs that value added to a variable. How do I do that? 

    I have tried strResult = ActiveDocument.FormFields("mag").Result but it errors with 5941 - The requested member of the collection does not exist

    Any help would be appreciated.  Thanks

     

     

    Thursday, May 19, 2011 12:06 AM

Answers

  • There are various different types of "Form Field" in Word, and each has to be accessed using a different set of objects, methods etc.

    For a Content Control, you need something more like

    ActiveDocument.ContentControls(1).Range.Text

    Unlike the FOrmField object, you can't use a text value (such as the tag) as the index to the COntentControls collection - if you want to do that, you have to iterate through the collection and deal with the possibility that Word does not enforce unique tag names. Something like:

    Dim bFound as Boolean

    Dim cc As Word.ContentControl

    bFound = False

    For each cc in ActiveDocument.ContentControls

      If cc.Tag = "mag" Then

        bFound = True

        strResult = cc.Range.Text

        Exit For

      End If

    Next

    ' Optionally do something if it wasn't found

    If not bFound Then

      '

    End If


    Peter Jamieson
    • Marked as answer by danee1 Thursday, May 19, 2011 12:03 PM
    Thursday, May 19, 2011 10:47 AM

All replies

  • There are various different types of "Form Field" in Word, and each has to be accessed using a different set of objects, methods etc.

    For a Content Control, you need something more like

    ActiveDocument.ContentControls(1).Range.Text

    Unlike the FOrmField object, you can't use a text value (such as the tag) as the index to the COntentControls collection - if you want to do that, you have to iterate through the collection and deal with the possibility that Word does not enforce unique tag names. Something like:

    Dim bFound as Boolean

    Dim cc As Word.ContentControl

    bFound = False

    For each cc in ActiveDocument.ContentControls

      If cc.Tag = "mag" Then

        bFound = True

        strResult = cc.Range.Text

        Exit For

      End If

    Next

    ' Optionally do something if it wasn't found

    If not bFound Then

      '

    End If


    Peter Jamieson
    • Marked as answer by danee1 Thursday, May 19, 2011 12:03 PM
    Thursday, May 19, 2011 10:47 AM
  • Many thanks Peter,

    I will need to find a good book if I am to do more with Word VBA.

     

    Kind Regards,
    Martyn

     

    Thursday, May 19, 2011 12:05 PM
  • When a CC is created it is assigned a unique ID.  You can get that ID by selecting the control and running:

     

    Sub ScratchMacro()
    Debug.Print Selection.Range.ContentControls(1).ID
    End Sub

    Here are some ways you can return the value of the first CC in a docuemnt (index = 1), or the CC with a unique ID, or the first CC titled "DemoCC" or the first CC tagged "DemoCCTag"

    Sub ReturnVvalueOofCC()
    MsgBox ActiveDocument.ContentControls(1).Range.Text
    MsgBox ActiveDocument.ContentControls("359095189").Range.Text
    MsgBox ActiveDocument.SelectContentControlsByTitle("DemoCC").Item(1).Range.Text
    MsgBox ActiveDocument.SelectContentControlsByTag("DemoCCTag").Item(1).Range.Text
    End Sub



    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm
    Sunday, May 22, 2011 11:44 PM
  • Thanks Greg,

    I appreciate the post and there are some great tips and examples on your website (although at 50 I doubt the Navy will have me back... sadly).

    Enjoy your day,
    Kind Regards,
    Martyn 

    Monday, May 23, 2011 7:23 AM