none
Extract all form field names RRS feed

  • Question

  • Hi,

    I need to do a task to extract all form field values from a fillable word 2003/2007.

    As my knowledge in word VSTO (or VBA) is limited, from my first idea,

    I think I need to get all form field control names in the document and then

    get the form field value one by one? Does any experts provide me some

    examples on how to read all form field controls name from a word document?

    By the way, I search in this forum that by calling this statement:

    ActiveDocument.SaveFormsData can provide a csv which contains

    all form fields data. I tried this in word 2007 but it seems the csv file

    contains a single row with all values on it, but does not show any

    information on which value matched with corresponding form field.

    Any enhance way to export both form field names and values into

    csv file?

    Thanks a lot for your help in advance,

    Raymond

    Wednesday, May 4, 2011 5:25 PM

Answers

  • Hi Raymond

    I'm going to move this question to the Word for Developers forum as you'll find a lot more folks there with the background for answering this question. (That forum did not exist at the time the question to which you refer was asked, I think).

    Your suspicion about the approach is correct, given the versions you need to support (*.doc files). One issue with what you want to do could be that it is never guaranteed that all form fields have a name. They will if they're inserted via the toolbar, but will not if the user copied/pasted and didn't explicitly set a name thereafter.

    could this be a problem for you if there is no Name? How would you want to handle that?

    The basic syntax:

    Sub LoopFormFields()
        Dim doc As word.Document
        Dim ffld As word.FormField
       
        Set doc = ActiveDocument
        For Each ffld In doc.FormFields
            Debug.Print ffld.Name, ffld.Result
        Next
       
    End Sub

    But if the user used copy/paste you could end up with a result like the following for three fields, one of which was copied from another:

    Text1         test
    Check1        0
    Text1         abc

    If there is no content, then the second Text1 (which is spurious) would not provide a name, but as soon as I typed into it, it reflected the name of the form field I copied.

    Form field names are actually bookmarks, and bookmark names must be unique in a document. In the scenario above that last field still doesn't have a name, but Word is picking up the source from which it was copied. So it's important that all fields DO have names if you're writing the information to some kind of database...


    Cindy Meister, VSTO/Word MVP
    Thursday, May 5, 2011 8:05 AM
    Moderator