![text form field word 2013 in vba text form field word 2013 in vba](https://legalofficeguru.com/wp-content/uploads/2017/04/Word-2016-bookmark-options-settings-display.png)
ffldName = fn.GetAttributes().FirstOrDefault().Value
![text form field word 2013 in vba text form field word 2013 in vba](http://sugartownpublishing.com/yahoo_site_admin/assets/images/Truchas-cover-for-Jannie-sm.26103416_std.jpg)
foreach (FormFieldData fd in fdata)įormFieldName fn = fd.Elements ().FirstOrDefault() While you can go to all the trouble of accessing it through GetAttributes (line commented out), the SDK knows a FormFieldName object has the attribute, and provides it for you as a property. The actual name of the form field is held in the attribute val. (FirstOrDefault means that, if there is no element of the kind specified null will be returned.)
#Text form field word 2013 in vba code
Since in a valid WordProcessing document a w:ffData element contains only one w:name child element we can get a FormFieldName object directly by specifying the code to return the FirstOrDefault element of all FormFieldName elements under w:ffData. If (tbType!=null & tbType.Val = "number")Īssuming we’re dealing with a textbox of type number, the next step is to get the form field’s name. TextBoxFormFieldType tbType = tb.Elements (Remember, the form field type is specified in the val attribute of the w:type element.) if (tb != null) In that case, we try to obtain the w:type element, which may not be present (and thus would return null), or may not be of type number. If the LINQ query was successful and the form field is a textbox, the TextInput object will not be null. TextInput tb = (TextInput)fd.Descendants().SingleOrDefault Note the explicit casting of the LINQ Query result to the type of object being populated. This time, we use LINQ to distinguish only the descendents of w:ffdata that are w:textInput objects by querying whether the name of the object (element) is textInput. The first task step is to pick out only the TextInput (textbox) type of formfield (rather than a dropdown or a checkbox) and determine whether it’s formatted as a number type. You can then loop through the sequence (it’s like working with a collection, but in LINQ it’s called a sequence) – every form field in the document. (In the case of form fields this will always be the main document part.) IEnumerable fdata = Rather than using SelectNodes and having to write out an XPath to get a set of elements, declare an IEnumerable object of the WordProcessing namespace (representing an element) and assign it the Descendents for that same type of object present in the document part. In contrast to the first set of code, this variation will pick up all the form fields in the document, not just the number types. No need to initiate an XML NameTable for the URIs to be used or to load an XML document – the Open XML SDK takes care of all that “plumbing”! The code can proceed directly to accessing the form field elements, starting with w:ffData that contains the child element w:name (the name of the form field). After that, the only similarity is the fact that the logic in both bases on how the Word Open XML is structured for form fields, as shown in the earlier post. And I was surprised at how much simpler the task actually was then using “pure” XML, once I understood the logic.īoth sets of code start with the same using clause for opening the Word document Open XML file and accessing the main document Part. It took me a while to get the feel for using the objects it exposes and working with the provided LINQ extensions, but in the end it worked.
#Text form field word 2013 in vba full
Once it was clear that my approach did work, it was time to see if I could do the same using the full capabilities of the Open XML SDK. As I felt more comfortable with old-fashioned XML coding, the tool presented in that post only leveraged the Open XML SDK for opening the file “package” and obtaining the required Part from the package. So I decided to try my hand at my first serious manipulation of Office Open XML files. As described in earlier posts, Word 2007 and Word 2010 don’t properly save number formats to number-type form fields.