When accessing Legacy Form Field values in my Word 2013 document like this (getting or setting):
\' get
myField = ActiveDocument.FormFields(\"myField\").Resu
This is a fantastic workaround if all you need to access is the Result field. Unfortunately, some of the form field properties are not accessible indirectly through the Bookmark collection, most notably StatusText. I am using formfields to build tests that are graded automatically in VBA code, and StatusText is the only field (to my knowledge) that allows you to store static (non-volatile) answer data in each form field.
Here's a sample of the code I am using. The following loop tests the Result fields to see if they match the answers stored in the StatusText field
For Each fld In oFormFields
strResult = fld.Result
strStatus = fld.StatusText
If strStatus = strResult Then
' color answer green to show that it was correct
fld.Range.Font.ColorIndex = wdBrightGreen
Else
' color answer red to show that it was wrong
fld.Range.Font.ColorIndex = wdRed
End If
Next fld
I see no way of replacing the form field references with bookmark references, but perhaps I am missing something.
I found a clue to the solution on the Word MVP Site. Like you pointed out, the issue is when you access the properties of a FormField
object, the focus is set to this form field. You can see this behavior even when browsing the form fields through the Locals Window in the VBA editor.
Instead of using the FormField
object, access the Result
property through the Bookmark
object.
Change this:
myField = ActiveDocument.FormFields("myField").Result
To this:
myField = ActiveDocument.Bookmarks("myField").Range.Fields(1).Result
Now you can access the value of this field without changing the focus in your document. To set a value on a field, you can use the Text
property.
ActiveDocument.Bookmarks("myField").Range.Fields(1).Result.Text = myValue
Hope this helps!! :-)