I'm building a form in a Word .docm (macro enabled Word 2013 doc) with the aim of programming an Access database to import data from completed forms. I've placed textBox and comboBox controls to receive user input, but I can't get the data back out.
The examples I've seen use the Document.FormFields
collection like so (in Word):
Dim fld as FormField
for each fld in ActiveDocument.FormFields
Debug.Print fld.Name & " - " & fld.Result.Text
next
However in my doc Document.FormFields
is empty, but Document.Fields
has 19 elements, which happens to be the number of controls in my form. That's great, except that I can't seem to get the name or value of any of the controls using a Field
object. Field.Result.Text
is always blank, and there is no Field.Name
attribute.
So what's the difference between Field
objects and FormField
objects, and why are my controls showing up in Fields
when all the examples I've seen use FormFields
?
Am I using the wrong form controls? There are three types (I hope I'm not the only one who thinks this is ridiculous) legacy controls, ActiveX controls, and content controls. I'm using the ActiveX type.
A few things...
In MS Word terms, a 'field' doesn't have to be a form field. E.g., an auto-updated date, linked graphic, page number, etc., are all types of 'field' (or at least, were until the most recent versions of Word).
For compatibility reasons, it can be better to avoid the ActiveX controls. E.g., the Mac version of Word doesn't support them.
For best compatibility I would personally stick to the traditional form controls. Instances are named according to their bookmark name, which is settable by right clicking the control and selecting Properties. In VBA, their data is then got via the FormFields collection; if you want the value for a specific field, use
Value = ActiveDocument.FormFields("MyFieldName").Result
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With