none
VB 2008 to read Word template form fields RRS feed

  • Question

  •  I have a 2003 Word doc with 20 form fields. 10 are form-fields in a table with 2 columns and 5 rows. The 2nd cell of each row has a drop-down form field. Using VB 2008, does anyone have a code example to read the contents of all these fields from  the Word doc ?

    basic code:

    Dim MSWord As New Word.Application
    Dim MyFirstFormObtainedValues as string
    Dim My2ndFormObtainedValues as string
     Dim WordDoc As New Word.Document
    WordDoc = MSWord.Documents.Open("C:\Demo.Doc")

    With WordDoc
    MyFirstFormObtainedValues=.????????????
    My2ndFormObtainedValues= .?????? TableCell form field value
    end with
    WordDoc .close

     
    • Edited by MrAnders0n Saturday, June 18, 2011 4:27 PM added code
    • Moved by Kee Poppy Monday, June 20, 2011 5:22 AM (From:Visual Basic General)
    Saturday, June 18, 2011 3:32 PM

Answers

  • Hi Anderson

    Before we get into details, I feel I should point out you shouldn't do the following with Word:

    Dim MSWord As New Word.Application

    You should do: Dim MSWord as Word.Application = New Word.Application()

    You can cycle through all the form fields in a document using something like

      Dim ffld as Word.FormField
      For each ffld in WordDoc.FormFields
         MessageBox.Show(ffld.Result)
      Next

    It's also possible to address form fields by name, if they have one. If you've used copy/paste to generate them, that may not be the case, however. A form field's name is also a bookmark, and these designations must be unique within a document. You can check for the names and assign them through the form field's Options dialog box. If a form field has a name, then:

      MyFirstFormObtainedValues = WordDoc.FormFields("TheName").Result


    Cindy Meister, VSTO/Word MVP
    Wednesday, June 22, 2011 12:41 PM
    Moderator