Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Word.Field class
- Package:
- word
Represents a field.
- Extends
Remarks
Important: To learn more about which fields can be inserted, see the Word.Range.insertField API introduced in requirement set 1.5. Support for managing fields is similar to what's available in the Word UI. However, the Word UI on the web primarily only supports fields as read-only (see Field codes in Word for the web). To learn more about Word UI clients that more fully support fields, see the product list at the beginning of Insert, edit, and view fields in Word.
Examples
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/manage-fields.yaml
// Gets the first field in the document.
await Word.run(async (context) => {
const field: Word.Field = context.document.body.fields.getFirstOrNullObject();
field.load(["code", "result", "locked", "type", "data", "kind"]);
await context.sync();
if (field.isNullObject) {
console.log("This document has no fields.");
} else {
console.log("Code of first field: " + field.code, "Result of first field: " + JSON.stringify(field.result), "Type of first field: " + field.type, "Is the first field locked? " + field.locked, "Kind of the first field: " + field.kind);
}
});
Properties
Specifies the field's code instruction.
The request context associated with the object. This connects the add-in's process to the Office host application's process.
Specifies data in a field of addin type. If the field isn't an addin type, it's null and it will throw a general exception when code attempts to set it.
Gets the field's kind.
Gets a LinkFormat object that represents the link options of the field.
Specifies whether the field is locked. true if the field is locked, false otherwise.
Gets an OleFormat object that represents the OLE characteristics (other than linking) for the field.
Gets the parent body of the field.
Gets the content control that contains the field. Throws an ItemNotFound error if there isn't a parent content control.
Gets the content control that contains the field. If there isn't a parent content control, then this method will return an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.
Gets the table that contains the field. Throws an ItemNotFound error if it isn't contained in a table.
Gets the table cell that contains the field. Throws an ItemNotFound error if it isn't contained in a table cell.
Gets the table cell that contains the field. If it isn't contained in a table cell, then this method will return an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.
Gets the table that contains the field. If it isn't contained in a table, then this method will return an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.
Gets the field's result data.
Specifies whether the field codes are displayed for the specified field. true if the field codes are displayed, false otherwise.
Gets the field's type.
Methods
Copies the field to the Clipboard.
Removes the field from the document and places it on the Clipboard.
Deletes the field.
Clicks the field.
Gets the next field. Throws an ItemNotFound error if this field is the last one.
Gets the next field. If this field is the last one, then this method will return an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.
Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.
Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.
Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.
Selects the field.
Selects the field.
Sets multiple properties of an object at the same time. You can pass either a plain object with the appropriate properties, or another API object of the same type.
Sets multiple properties on the object at the same time, based on an existing loaded object.
Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify, in turn, calls the toJSON method of the object that's passed to it.) Whereas the original Word.Field object is an API object, the toJSON method returns a plain JavaScript object (typed as Word.Interfaces.FieldData) that contains shallow copies of any loaded child properties from the original object.
Track the object for automatic adjustment based on surrounding changes in the document. This call is a shorthand for context.trackedObjects.add(thisObject). If you're using this object across .sync calls and outside the sequential execution of a ".run" batch, and get an "InvalidObjectPath" error when setting a property or invoking a method on the object, you need to add the object to the tracked object collection when the object was first created. If this object is part of a collection, you should also track the parent collection.
Replaces the field with its most recent result.
Release the memory associated with this object, if it has previously been tracked. This call is shorthand for context.trackedObjects.remove(thisObject). Having many tracked objects slows down the host application, so please remember to free any objects you add, once you're done using them. You'll need to call context.sync() before the memory release takes effect.
Updates the field.
Saves the changes made to the results of an INCLUDETEXT field back to the source document.
Property Details
code
Specifies the field's code instruction.
code: string;
Property Value
string
Remarks
Note: The ability to set the code was introduced in WordApi 1.5.
Examples
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/manage-fields.yaml
// Gets the first field in the document.
await Word.run(async (context) => {
const field: Word.Field = context.document.body.fields.getFirstOrNullObject();
field.load(["code", "result", "locked", "type", "data", "kind"]);
await context.sync();
if (field.isNullObject) {
console.log("This document has no fields.");
} else {
console.log("Code of first field: " + field.code, "Result of first field: " + JSON.stringify(field.result), "Type of first field: " + field.type, "Is the first field locked? " + field.locked, "Kind of the first field: " + field.kind);
}
});
context
The request context associated with the object. This connects the add-in's process to the Office host application's process.
context: RequestContext;
Property Value
data
Specifies data in a field of addin type. If the field isn't an addin type, it's null and it will throw a general exception when code attempts to set it.
data: string;
Property Value
string
Remarks
kind
Gets the field's kind.
readonly kind: Word.FieldKind | "None" | "Hot" | "Warm" | "Cold";
Property Value
Word.FieldKind | "None" | "Hot" | "Warm" | "Cold"
Remarks
Examples
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/manage-fields.yaml
// Gets the first field in the document.
await Word.run(async (context) => {
const field: Word.Field = context.document.body.fields.getFirstOrNullObject();
field.load(["code", "result", "locked", "type", "data", "kind"]);
await context.sync();
if (field.isNullObject) {
console.log("This document has no fields.");
} else {
console.log("Code of first field: " + field.code, "Result of first field: " + JSON.stringify(field.result), "Type of first field: " + field.type, "Is the first field locked? " + field.locked, "Kind of the first field: " + field.kind);
}
});
linkFormat
Gets a LinkFormat object that represents the link options of the field.
readonly linkFormat: Word.LinkFormat;
Property Value
Remarks
locked
Specifies whether the field is locked. true if the field is locked, false otherwise.
locked: boolean;
Property Value
boolean
Remarks
Examples
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/manage-fields.yaml
// Gets the first field in the selection and toggles between setting it to locked or unlocked.
await Word.run(async (context) => {
let field = context.document.getSelection().fields.getFirstOrNullObject();
field.load(["code", "result", "type", "locked"]);
await context.sync();
if (field.isNullObject) {
console.log("The selection has no fields.");
} else {
console.log(`The first field in the selection is currently ${field.locked ? "locked" : "unlocked"}.`);
field.locked = !field.locked;
await context.sync();
console.log(`The first field in the selection is now ${field.locked ? "locked" : "unlocked"}.`);
}
});
oleFormat
Gets an OleFormat object that represents the OLE characteristics (other than linking) for the field.
readonly oleFormat: Word.OleFormat;
Property Value
Remarks
parentBody
Gets the parent body of the field.
readonly parentBody: Word.Body;
Property Value
Remarks
Examples
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/manage-fields.yaml
// Gets the parent body of the first field in the document.
await Word.run(async (context) => {
const field: Word.Field = context.document.body.fields.getFirstOrNullObject();
field.load("parentBody/text");
await context.sync();
if (field.isNullObject) {
console.log("This document has no fields.");
} else {
const parentBody: Word.Body = field.parentBody;
console.log("Text of first field's parent body: " + JSON.stringify(parentBody.text));
}
});
parentContentControl
Gets the content control that contains the field. Throws an ItemNotFound error if there isn't a parent content control.
readonly parentContentControl: Word.ContentControl;
Property Value
Remarks
parentContentControlOrNullObject
Gets the content control that contains the field. If there isn't a parent content control, then this method will return an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.
readonly parentContentControlOrNullObject: Word.ContentControl;
Property Value
Remarks
parentTable
Gets the table that contains the field. Throws an ItemNotFound error if it isn't contained in a table.
readonly parentTable: Word.Table;
Property Value
Remarks
parentTableCell
Gets the table cell that contains the field. Throws an ItemNotFound error if it isn't contained in a table cell.
readonly parentTableCell: Word.TableCell;
Property Value
Remarks
parentTableCellOrNullObject
Gets the table cell that contains the field. If it isn't contained in a table cell, then this method will return an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.
readonly parentTableCellOrNullObject: Word.TableCell;
Property Value
Remarks
parentTableOrNullObject
Gets the table that contains the field. If it isn't contained in a table, then this method will return an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.
readonly parentTableOrNullObject: Word.Table;
Property Value
Remarks
result
Gets the field's result data.
readonly result: Word.Range;
Property Value
Remarks
Examples
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/manage-fields.yaml
// Gets the first field in the document.
await Word.run(async (context) => {
const field: Word.Field = context.document.body.fields.getFirstOrNullObject();
field.load(["code", "result", "locked", "type", "data", "kind"]);
await context.sync();
if (field.isNullObject) {
console.log("This document has no fields.");
} else {
console.log("Code of first field: " + field.code, "Result of first field: " + JSON.stringify(field.result), "Type of first field: " + field.type, "Is the first field locked? " + field.locked, "Kind of the first field: " + field.kind);
}
});
showCodes
Specifies whether the field codes are displayed for the specified field. true if the field codes are displayed, false otherwise.
showCodes: boolean;
Property Value
boolean
Remarks
type
Gets the field's type.
readonly type: Word.FieldType | "Addin" | "AddressBlock" | "Advance" | "Ask" | "Author" | "AutoText" | "AutoTextList" | "BarCode" | "Bibliography" | "BidiOutline" | "Citation" | "Comments" | "Compare" | "CreateDate" | "Data" | "Database" | "Date" | "DisplayBarcode" | "DocProperty" | "DocVariable" | "EditTime" | "Embedded" | "EQ" | "Expression" | "FileName" | "FileSize" | "FillIn" | "FormCheckbox" | "FormDropdown" | "FormText" | "GotoButton" | "GreetingLine" | "Hyperlink" | "If" | "Import" | "Include" | "IncludePicture" | "IncludeText" | "Index" | "Info" | "Keywords" | "LastSavedBy" | "Link" | "ListNum" | "MacroButton" | "MergeBarcode" | "MergeField" | "MergeRec" | "MergeSeq" | "Next" | "NextIf" | "NoteRef" | "NumChars" | "NumPages" | "NumWords" | "OCX" | "Page" | "PageRef" | "Print" | "PrintDate" | "Private" | "Quote" | "RD" | "Ref" | "RevNum" | "SaveDate" | "Section" | "SectionPages" | "Seq" | "Set" | "Shape" | "SkipIf" | "StyleRef" | "Subject" | "Subscriber" | "Symbol" | "TA" | "TC" | "Template" | "Time" | "Title" | "TOA" | "TOC" | "UserAddress" | "UserInitials" | "UserName" | "XE" | "Empty" | "Others" | "Undefined";
Property Value
Word.FieldType | "Addin" | "AddressBlock" | "Advance" | "Ask" | "Author" | "AutoText" | "AutoTextList" | "BarCode" | "Bibliography" | "BidiOutline" | "Citation" | "Comments" | "Compare" | "CreateDate" | "Data" | "Database" | "Date" | "DisplayBarcode" | "DocProperty" | "DocVariable" | "EditTime" | "Embedded" | "EQ" | "Expression" | "FileName" | "FileSize" | "FillIn" | "FormCheckbox" | "FormDropdown" | "FormText" | "GotoButton" | "GreetingLine" | "Hyperlink" | "If" | "Import" | "Include" | "IncludePicture" | "IncludeText" | "Index" | "Info" | "Keywords" | "LastSavedBy" | "Link" | "ListNum" | "MacroButton" | "MergeBarcode" | "MergeField" | "MergeRec" | "MergeSeq" | "Next" | "NextIf" | "NoteRef" | "NumChars" | "NumPages" | "NumWords" | "OCX" | "Page" | "PageRef" | "Print" | "PrintDate" | "Private" | "Quote" | "RD" | "Ref" | "RevNum" | "SaveDate" | "Section" | "SectionPages" | "Seq" | "Set" | "Shape" | "SkipIf" | "StyleRef" | "Subject" | "Subscriber" | "Symbol" | "TA" | "TC" | "Template" | "Time" | "Title" | "TOA" | "TOC" | "UserAddress" | "UserInitials" | "UserName" | "XE" | "Empty" | "Others" | "Undefined"
Remarks
Examples
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/manage-fields.yaml
// Gets the first field in the document.
await Word.run(async (context) => {
const field: Word.Field = context.document.body.fields.getFirstOrNullObject();
field.load(["code", "result", "locked", "type", "data", "kind"]);
await context.sync();
if (field.isNullObject) {
console.log("This document has no fields.");
} else {
console.log("Code of first field: " + field.code, "Result of first field: " + JSON.stringify(field.result), "Type of first field: " + field.type, "Is the first field locked? " + field.locked, "Kind of the first field: " + field.kind);
}
});
Method Details
copyToClipboard()
Copies the field to the Clipboard.
copyToClipboard(): void;
Returns
void
Remarks
cut()
Removes the field from the document and places it on the Clipboard.
cut(): void;
Returns
void
Remarks
delete()
Deletes the field.
delete(): void;
Returns
void
Remarks
Examples
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/manage-fields.yaml
// Deletes the first field in the document.
await Word.run(async (context) => {
const field: Word.Field = context.document.body.fields.getFirstOrNullObject();
field.load();
await context.sync();
if (field.isNullObject) {
console.log("This document has no fields.");
} else {
field.delete();
await context.sync();
console.log("The first field in the document was deleted.");
}
});
doClick()
getNext()
Gets the next field. Throws an ItemNotFound error if this field is the last one.
getNext(): Word.Field;
Returns
Remarks
getNextOrNullObject()
Gets the next field. If this field is the last one, then this method will return an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.
getNextOrNullObject(): Word.Field;
Returns
Remarks
load(options)
Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.
load(options?: Word.Interfaces.FieldLoadOptions): Word.Field;
Parameters
- options
- Word.Interfaces.FieldLoadOptions
Provides options for which properties of the object to load.
Returns
load(propertyNames)
Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.
load(propertyNames?: string | string[]): Word.Field;
Parameters
- propertyNames
-
string | string[]
A comma-delimited string or an array of strings that specify the properties to load.
Returns
load(propertyNamesAndPaths)
Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.
load(propertyNamesAndPaths?: {
select?: string;
expand?: string;
}): Word.Field;
Parameters
- propertyNamesAndPaths
-
{ select?: string; expand?: string; }
propertyNamesAndPaths.select is a comma-delimited string that specifies the properties to load, and propertyNamesAndPaths.expand is a comma-delimited string that specifies the navigation properties to load.
Returns
select(selectionMode)
Selects the field.
select(selectionMode?: Word.SelectionMode): void;
Parameters
- selectionMode
- Word.SelectionMode
Optional. The selection mode must be select, start, or end. select is the default.
Returns
void
Remarks
Examples
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/manage-fields.yaml
// Gets and updates the first field in the selection.
await Word.run(async (context) => {
let field = context.document.getSelection().fields.getFirstOrNullObject();
field.load(["code", "result", "type", "locked"]);
await context.sync();
if (field.isNullObject) {
console.log("No field in selection.");
} else {
console.log("Before updating:", "Code of first field: " + field.code, "Result of first field: " + JSON.stringify(field.result));
field.updateResult();
field.select();
await context.sync();
field.load(["code", "result"]);
await context.sync();
console.log("After updating:", "Code of first field: " + field.code, "Result of first field: " + JSON.stringify(field.result));
}
});
select(selectionMode)
Selects the field.
select(selectionMode?: "Select" | "Start" | "End"): void;
Parameters
- selectionMode
-
"Select" | "Start" | "End"
Optional. The selection mode must be select, start, or end. select is the default.
Returns
void
Remarks
set(properties, options)
Sets multiple properties of an object at the same time. You can pass either a plain object with the appropriate properties, or another API object of the same type.
set(properties: Interfaces.FieldUpdateData, options?: OfficeExtension.UpdateOptions): void;
Parameters
- properties
- Word.Interfaces.FieldUpdateData
A JavaScript object with properties that are structured isomorphically to the properties of the object on which the method is called.
- options
- OfficeExtension.UpdateOptions
Provides an option to suppress errors if the properties object tries to set any read-only properties.
Returns
void
set(properties)
Sets multiple properties on the object at the same time, based on an existing loaded object.
set(properties: Word.Field): void;
Parameters
- properties
- Word.Field
Returns
void
toJSON()
Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify, in turn, calls the toJSON method of the object that's passed to it.) Whereas the original Word.Field object is an API object, the toJSON method returns a plain JavaScript object (typed as Word.Interfaces.FieldData) that contains shallow copies of any loaded child properties from the original object.
toJSON(): Word.Interfaces.FieldData;
Returns
track()
Track the object for automatic adjustment based on surrounding changes in the document. This call is a shorthand for context.trackedObjects.add(thisObject). If you're using this object across .sync calls and outside the sequential execution of a ".run" batch, and get an "InvalidObjectPath" error when setting a property or invoking a method on the object, you need to add the object to the tracked object collection when the object was first created. If this object is part of a collection, you should also track the parent collection.
track(): Word.Field;
Returns
unlink()
Replaces the field with its most recent result.
unlink(): void;
Returns
void
Remarks
untrack()
Release the memory associated with this object, if it has previously been tracked. This call is shorthand for context.trackedObjects.remove(thisObject). Having many tracked objects slows down the host application, so please remember to free any objects you add, once you're done using them. You'll need to call context.sync() before the memory release takes effect.
untrack(): Word.Field;
Returns
updateResult()
Updates the field.
updateResult(): void;
Returns
void
Remarks
Examples
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/manage-fields.yaml
// Gets and updates the first field in the selection.
await Word.run(async (context) => {
let field = context.document.getSelection().fields.getFirstOrNullObject();
field.load(["code", "result", "type", "locked"]);
await context.sync();
if (field.isNullObject) {
console.log("No field in selection.");
} else {
console.log("Before updating:", "Code of first field: " + field.code, "Result of first field: " + JSON.stringify(field.result));
field.updateResult();
field.select();
await context.sync();
field.load(["code", "result"]);
await context.sync();
console.log("After updating:", "Code of first field: " + field.code, "Result of first field: " + JSON.stringify(field.result));
}
});
updateSource()
Saves the changes made to the results of an INCLUDETEXT field back to the source document.
updateSource(): void;
Returns
void
Remarks
Office Add-ins