Fields in TypeScript DOCX Editor
28 Aug 20262 minutes to read
TypeScript DOCX Editor (Document Editor) has preservation support for all types of fields in an existing Word document without any data loss.
Adding Fields
You can add a field to the document by using the insertField method in the Editor module.
The following example code illustrates how to insert a merge field programmatically by providing the field code and field result.
let fieldCode: string = 'MERGEFIELD First Name \\* MERGEFORMAT ';
let fieldResult: string = '«First Name»';
documenteditor.editor.insertField(fieldCode, fieldResult);The DOCX Editor does not validate/process the field code/field result. It simply inserts the field with the specified field information.
Update fields
The DOCX Editor provides support for updating bookmark cross-reference fields. The following example code illustrates how to update bookmark cross-reference fields.
//Update all the bookmark cross reference field in the document.
documentEditor.updateFields();Bookmark cross-reference fields can be updated through the UI by using the Update Fields option in the Toolbar.

The following types of fields are automatically updated in the DOCX Editor.
- NUMPAGES
- SECTION
- PAGE
Get field info
You can get the field code and field result of the current selected field by using the getFieldInfo method in the Selection module.
//Gets the field information of the selected field.
let fieldInfo: FieldInfo = documentEditor.selection.getFieldInfo();For nested fields, this method returns combined field code and result.
Set field info
You can modify the field code and field result of the current selected field by using the setFieldInfo method in the Editor module.
//Gets the field information for the selected field.
let fieldInfo: FieldInfo = documentEditor.selection.getFieldInfo();
//Modify field code
fieldInfo.code = 'MERGEFIELD First Name \\* MERGEFORMAT ';
//Modify field result
fieldInfo.result = '«First Name»';
//Modify field code and result of the current selected field.
documentEditor.editor.setFieldInfo(fieldInfo);For a nested field, the entire field gets replaced completely with the specified field information.