Skip to content

Commit 0b30088

Browse files
[Word] (Fields) Add article (#5090)
* [Word] (Fields) Add article * Add to TOC * Include snippet that uses Date field * Add code samples and descriptions * Tweak samples * Add remaining code samples * Apply suggestions from code review Co-authored-by: Linda Cannon <[email protected]> --------- Co-authored-by: Linda Cannon <[email protected]>
1 parent d1544d2 commit 0b30088

File tree

2 files changed

+205
-0
lines changed

2 files changed

+205
-0
lines changed

docs/toc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,9 @@ items:
962962
- name: Read and write data to the active selection
963963
href: develop/read-and-write-data-to-the-active-selection-in-a-document-or-spreadsheet.md
964964
displayName: Word
965+
- name: Use fields
966+
href: word/fields-guidance.md
967+
displayName: Word
965968
- name: Use search options to find text
966969
href: word/search-option-guidance.md
967970
displayName: Word

docs/word/fields-guidance.md

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
---
2+
title: Use fields in your Word add-in
3+
description: Learn to use fields in your Word add-in.
4+
ms.date: 03/18/2025
5+
ms.localizationpriority: medium
6+
---
7+
8+
# Use fields in your Word add-in
9+
10+
A [field](https://support.microsoft.com/office/c429bbb0-8669-48a7-bd24-bab6ba6b06bb) in a Word document is a placeholder. It allows you to provide instructions for the content instead of the content itself. You can use fields to create and format a Word template. Word documents support a number of [field types](https://support.microsoft.com/office/1ad6d91a-55a7-4a8d-b535-cf7888659a51), many with associated parameters for configuring the field. However, Word on the web generally doesn't support adding or editing fields through the UI. For more information, see [Field codes in Word for the web](https://support.microsoft.com/office/d8f46094-13c3-4966-98c3-259748f3caf1).
11+
12+
Starting from the WordApi 1.5 requirement set, Word JavaScript APIs allow you to manage fields in your Word add-in. In all platforms, you can get existing fields. You can insert, update, and delete fields in platforms that support those capabilities.
13+
14+
The following sections discuss several of the most frequently used field types: Addin, Date, Hyperlink, and TOC (Table of Contents).
15+
16+
## Addin field
17+
18+
The Addin field is meant to store add-in data that's hidden from the Word user interface, regardless of whether fields in the document are set to show or hide its content. The Addin field isn't available in the Word UI's **Field** dialog box. Use the API to insert the Addin field type and set the field's data.
19+
20+
The following code sample shows how to insert an Addin field before the cursor location or your selection in the Word document.
21+
22+
```javascript
23+
// Inserts an Addin field before selection.
24+
async function rangeInsertAddinField() {
25+
await Word.run(async (context) => {
26+
let range = context.document.getSelection().getRange();
27+
const field = range.insertField(Word.InsertLocation.before, Word.FieldType.addin);
28+
field.load("result,code");
29+
await context.sync();
30+
31+
if (field.isNullObject) {
32+
console.log("There are no fields in this document.");
33+
} else {
34+
console.log("Code of the field: " + field.code);
35+
console.log("Result of the field: " + JSON.stringify(field.result));
36+
}
37+
});
38+
}
39+
```
40+
41+
The following code sample shows how to get the first Addin field found in a document then set that field's data property.
42+
43+
```javascript
44+
// Gets the first Addin field in the document and sets its data.
45+
async function getFirstAddinFieldAndSetData() {
46+
await Word.run(async (context) => {
47+
let myFieldTypes = new Array();
48+
myFieldTypes[0] = Word.FieldType.addin;
49+
const addinFields = context.document.body.fields.getByTypes(myFieldTypes);
50+
let fields = addinFields.load("items");
51+
await context.sync();
52+
53+
if (fields.items.length === 0) {
54+
console.log("No Addin fields in this document.");
55+
} else {
56+
fields.load();
57+
await context.sync();
58+
59+
const firstAddinField = fields.items[0];
60+
firstAddinField.load("code,result,data");
61+
await context.sync();
62+
63+
console.log("The data of the Addin field before being set:", firstAddinField.data);
64+
const data = "Insert your data here";
65+
//const data = $("#input-reference").val(); // Or get user data from your add-in's UI.
66+
firstAddinField.data = data;
67+
firstAddinField.load("data");
68+
await context.sync();
69+
70+
console.log("The data of the Addin field after being set:", firstAddinField.data);
71+
}
72+
});
73+
}
74+
```
75+
76+
## Date field
77+
78+
The Date field inserts the current date according to the format you specify. You can toggle between displaying the date or the field code by setting the `showCodes` field property to `false` or `true` respectively.
79+
80+
The following code sample shows how to insert a Date field before the cursor location or your selection in the Word document.
81+
82+
```javascript
83+
// Inserts a Date field before selection.
84+
async function rangeInsertDateField() {
85+
await Word.run(async (context) => {
86+
let range = context.document.getSelection().getRange();
87+
const field = range.insertField(
88+
Word.InsertLocation.before,
89+
Word.FieldType.date,
90+
'\\@ "M/d/yyyy h:mm am/pm"',
91+
true
92+
);
93+
field.load("result,code");
94+
await context.sync();
95+
96+
if (field.isNullObject) {
97+
console.warn("The field wasn't inserted as expected.");
98+
} else {
99+
console.log("Code of the field: " + field.code);
100+
console.log("Result of the field: " + JSON.stringify(field.result));
101+
}
102+
});
103+
}
104+
```
105+
106+
### Further reading
107+
108+
- [Manage Fields code sample](https://github.com/OfficeDev/office-js-snippets/blob/prod/samples/word/50-document/manage-fields.yaml)
109+
- [Field codes: Date field](https://support.microsoft.com/office/d0c7e1f1-a66a-4b02-a3f4-1a1c56891306)
110+
111+
## Hyperlink field
112+
113+
The Hyperlink field inserts the address of either a location in the same document or an external location such as a webpage. When the user selects it, they're navigated to the specified location. You can toggle between displaying the hyperlink address or the field code by setting the `showCodes` field property to `false` or `true` respectively.
114+
115+
The following code sample shows how to insert a Hyperlink field before the cursor location or your selection in the Word document.
116+
117+
```javascript
118+
// Inserts a Hyperlink field before selection.
119+
async function rangeInsertHyperlinkField() {
120+
await Word.run(async (context) => {
121+
let range = context.document.getSelection().getRange();
122+
const field = range.insertField(
123+
Word.InsertLocation.before,
124+
Word.FieldType.hyperlink,
125+
"https://bing.com",
126+
true
127+
);
128+
field.load("result,code");
129+
await context.sync();
130+
131+
if (field.isNullObject) {
132+
console.warn("The field wasn't inserted as expected.");
133+
} else {
134+
console.log("Code of the field: " + field.code);
135+
console.log("Result of the field: " + JSON.stringify(field.result));
136+
}
137+
});
138+
}
139+
```
140+
141+
### Further reading
142+
143+
- [Field codes: Hyperlink field](https://support.microsoft.com/office/864f8577-eb2a-4e55-8c90-40631748ef53)
144+
145+
## TOC (Table of Contents) field
146+
147+
The TOC field inserts a table of contents, which lists certain areas of a document, like headings. You can toggle between displaying the table of contents or the field code by setting the `showCodes` field property to `false` or `true` respectively.
148+
149+
The following code sample shows how to insert a TOC field at the cursor location or replace your current selection in the Word document.
150+
151+
```javascript
152+
/**
153+
1. Run setup.
154+
1. Select "[To place table of contents]" paragraph.
155+
1. Run rangeInsertTOCField.
156+
*/
157+
158+
// Inserts a TOC (table of contents) field replacing selection.
159+
async function rangeInsertTOCField() {
160+
await Word.run(async (context) => {
161+
let range = context.document.getSelection().getRange();
162+
const field = range.insertField(
163+
Word.InsertLocation.replace,
164+
Word.FieldType.toc
165+
);
166+
field.load("result,code");
167+
await context.sync();
168+
169+
if (field.isNullObject) {
170+
console.warn("The field wasn't inserted as expected.");
171+
} else {
172+
console.log("Code of the field: " + field.code);
173+
console.log("Result of the field: " + JSON.stringify(field.result));
174+
}
175+
});
176+
}
177+
178+
// Prep document so there'll be elements that could be included in a table of contents.
179+
async function setup() {
180+
await Word.run(async (context) => {
181+
const body: Word.Body = context.document.body;
182+
body.clear();
183+
body.insertParagraph("Document title", "End").styleBuiltIn = Word.BuiltInStyleName.title;
184+
body.insertParagraph("[To place table of contents]", "End").styleBuiltIn = Word.BuiltInStyleName.normal;
185+
body.insertParagraph("Introduction", "End").styleBuiltIn = Word.BuiltInStyleName.heading1;
186+
body.insertParagraph("Paragraph 1", "End").styleBuiltIn = Word.BuiltInStyleName.normal;
187+
body.insertParagraph("Topic 1", "End").styleBuiltIn = Word.BuiltInStyleName.heading1;
188+
body.insertParagraph("Paragraph 2", "End").styleBuiltIn = Word.BuiltInStyleName.normal;
189+
body.insertParagraph("Topic 2", "End").styleBuiltIn = Word.BuiltInStyleName.heading1;
190+
body.insertParagraph("Paragraph 3", "End").styleBuiltIn = Word.BuiltInStyleName.normal;
191+
});
192+
}
193+
```
194+
195+
### Further reading
196+
197+
- [Field codes: TOC (Table of Contents) field](https://support.microsoft.com/office/1f538bc4-60e6-4854-9f64-67754d78d05c)
198+
199+
## See also
200+
201+
- [Field codes in Word for the web](https://support.microsoft.com/office/d8f46094-13c3-4966-98c3-259748f3caf1)
202+
- [Insert, edit, and view fields in Word](https://support.microsoft.com/office/c429bbb0-8669-48a7-bd24-bab6ba6b06bb)

0 commit comments

Comments
 (0)