A field is text that Word works out: a page number, a date, a table of contents. The file stores the instruction and the last result, so a reader that does not understand fields still has text to show.
Simple fields
w:fldSimple holds the instruction in an attribute and the result as runs.
<w:fldSimple w:instr=" PAGE ">
<w:r><w:t>4</w:t></w:r>
</w:fldSimple>
Complex fields
A complex field is spread over runs, marked by three w:fldChar elements. It
can span paragraphs, and fields can nest: a table of contents is one field whose result is
many paragraphs, each with fields of its own.
<w:r><w:fldChar w:fldCharType="begin"/></w:r>
<w:r><w:instrText xml:space="preserve"> NUMPAGES </w:instrText></w:r>
<w:r><w:fldChar w:fldCharType="separate"/></w:r>
<w:r><w:t>12</w:t></w:r>
<w:r><w:fldChar w:fldCharType="end"/></w:r>
| Attribute of w:fldChar | Meaning |
|---|---|
w:fldCharType | begin, separate or end. A field with no separator has no stored result. |
w:dirty | The result is out of date and should be recalculated before display, if the reader can. Default: false. Ignorable on any w:fldChar that is not begin. |
w:fldLock | The result must not be recalculated, even when all fields are updated. Default: false. Ignorable on any w:fldChar that is not begin. |
Rules from 17.16.18: the separate character is optional, and leaving it out
means the field is all instruction with no result. A field that is not closed by the end
of its story is not a field at all: the characters are processed as if the field
characters were absent, so the instruction runs stay hidden and the result runs show as
ordinary text. w:fldSimple (17.16.19) has the same w:dirty and
w:fldLock, plus w:instr, the instruction as a string.
The viewer here pushes a field at each begin, hides runs until
separate, shows runs until end, and nests, so an unclosed field
behaves as the standard says. It substitutes page numbers for PAGE and
NUMPAGES fields in headers and footers and shows the stored result for every
other field.
Common field codes
The instruction starts with the field’s name, then arguments and switches (a backslash and a letter).
| Field | Result |
|---|---|
PAGE, NUMPAGES, SECTION | Page number, number of pages, section number. |
DATE, TIME, CREATEDATE, SAVEDATE, PRINTDATE | Dates and times, with a format in the \@ switch. |
TOC | A table of contents, from heading styles or outline levels. |
HYPERLINK | A link: a target in quotes, and \l for a bookmark. |
REF, PAGEREF | The text of, or page number of, a bookmark. |
SEQ | The next number in a named sequence, as used for captions. |
STYLEREF | The text of the nearest paragraph in a given style, often used in headers. |
MERGEFIELD | A mail merge field. |
AUTHOR, TITLE, FILENAME, DOCPROPERTY | Values from the document’s properties and its file. |
FORMTEXT, FORMCHECKBOX, FORMDROPDOWN | Legacy form fields, with their state in w:ffData on the opening w:fldChar. |
IF | Either of two texts, by a comparison. |
The standard defines 72 field codes in 17.16.5.1 to 17.16.5.72 (the last
is XE), and the table above is only the common ones. Word also accepts
switches the standard does not define: SECTIONPAGES in the standard is the
page number within the section, but Word gives the number of pages in the section, and
accepts a numeric-formatting switch (MS-OI29500 2.1.499). A PAGEREF result
can contain “above”, “below” or “on page” in Word’s own wording (2.1.490), and the
\n, \r, \w and \l switches of
STYLEREF are only partly defined (2.1.502).
Reading a file does not run fields. Word recalculates some kinds when printing or opening, or when told to update them; other readers show the stored result.
Hyperlinks
w:hyperlink wraps the runs of the link’s text.
| Attribute | Meaning |
|---|---|
r:id | A relationship of type hyperlink: the target URI, usually with TargetMode="External". |
w:anchor | The name of a bookmark in this document. If r:id is also present, w:anchor is ignored. A missing bookmark, or no w:anchor at all, means the start of the document. |
w:docLocation | A location in the target document that has no bookmark, as an opaque string. Ignorable when w:anchor is also given. How it maps to text is outside the standard. |
w:tgtFrame | The frame of an HTML frameset to open the target in: _top, _self, _parent, _blank, or a frame name. A value that does not begin with a letter is ignored, and so is the whole attribute outside a frameset. |
w:tooltip | Text to show on hover. How it is shown is up to the application. |
w:history | Whether to add the target to the list of viewed links when it is invoked. Default: false. |
All of these are in 17.16.22. The viewer on this site follows r:id first, then
w:anchor, and does not use w:docLocation, w:tgtFrame or
w:history: external addresses open in a new tab, whatever the frame.
A link can also be a complex field with the HYPERLINK code; the result is the
clickable text.
The explorer on this site makes only web and mail addresses clickable, opens them in a new tab, and never fetches anything itself. Other targets are listed but not followed.
Bookmarks
w:bookmarkStart has a w:id and a w:name;
w:bookmarkEnd repeats the ID. Between them is the bookmarked range, which can
cover several paragraphs. A hyperlink or REF field refers to the name. Word’s
contents tables use bookmarks named _Toc and a number, and
_GoBack is the place the editing cursor was last.
Legacy form fields in detail (w:ffData)
w:ffData (17.16.17) sits inside the begin w:fldChar
(the schema lets w:fldChar hold one of w:fldData, w:ffData
or w:numberingChange). It belongs only to the three field codes
FORMCHECKBOX (17.16.5.20), FORMDROPDOWN (17.16.5.21) and
FORMTEXT (17.16.5.22); on any other field code the document is non-conformant.
If it is left out, the defaults below apply. In the schema (CT_FFData) the
children are a repeating choice, so they may come in any order. Every element named here
is in the w: namespace.
| Child of w:ffData | Section | Meaning and default |
|---|---|---|
name | 17.16.27 | w:val, ST_FFName, at most 65 characters in the schema. No name when absent. |
enabled | 17.16.14 | Boolean (17.17.4: no w:val means true). Has an effect only when the settings part allows form filling through w:documentProtection. Absent: enabled. |
calcOnExit | 17.16.6 | Boolean. True: all fields of the document are recalculated when this field's contents change. Absent: they are not. |
entryMacro, exitMacro | 17.16.15, 17.16.16 | w:val, ST_MacroName, at most 33 characters: a subroutine to run on entering or leaving the field. A macro that cannot be found is ignored but must not be lost on saving. |
helpText, statusText | 17.16.21, 17.16.31 | w:type is text (default) or autoText; w:val is the text itself, or the name of a glossary entry holding it. The schema limits the help text to 256 and the status text to 140 characters. How they are shown is not defined. |
label | 17.16.24 | w:val is the w:id of a structured document tag whose content is the label. Several labels are ordered from most general to most specific. Unresolvable: no label. |
tabIndex | 17.16.32 | Non-zero values are visited first, lowest to highest; equal values and the fields with none or 0 follow in document order. |
checkBox | 17.16.7 | Children in this order: size (17.16.29, w:val in half-points, the exact size) or sizeAuto (17.16.30, use the size of the field character's run); then default (17.16.12, boolean, absent means unchecked); then checked (17.16.8, boolean, the current state; absent means the state is the default). Ignorable on a field that is not FORMCHECKBOX. |
ddList | 17.16.9 | Children in this order: result (17.16.28, zero-based index of the selected entry), default (17.16.11, zero-based index shown when there is no result), then any number of listEntry (17.16.25, w:val string; document order is list order). An index that is negative or past the end is ignored: the result falls back to the default, and the default to entry 0. |
textInput | 17.16.33 | Children in this order: type (17.16.34), default (17.16.10, a string: shown when the field's own content is empty, and the calculation itself when the type is calculated), maxLength (17.16.26, a number; text already longer is not an error, but no more characters may be added), format (17.16.20). |
w:type (ST_FFTextType, 17.18.28) is regular (default), number,
date, currentTime, currentDate or calculated. It must
not stop any content loading; it tells how w:format is read and may be used to
refuse illegal input. The format is a date picture (see below) for date,
currentDate and currentTime, a numeric picture for number and
calculated, and for regular one of four text formats: Uppercase,
Lowercase, First capital (first letter of the first word), Title case (first letter of each word).
Viewer: the page view draws a form field whose stored result holds no text from its
w:ffData: a checkbox as a box (checked, else default) with the ballot-box characters
U+2612 and U+2610, a drop-down list as the selected entry, and a text box as its default string.
A field with a stored result shows that result, as Word does. The macro names, help and status text,
label, tab order, maximum length and formats are not used by a viewer that cannot edit; they are
not shown either.
Formulas and expressions (17.16.3)
A formula field (=) holds an expression built from constants, bookmarks, operators,
functions, table cell references and nested fields, with parentheses for grouping. All terms are real
numbers: infinities and NaN do not exist, so 1/3 is 0.33.
- Constants (17.16.3.1): a number such as
1234,1234.560,1234.,.1234. No exponents. - Bookmarks (17.16.3.2): a bookmark whose text is an expression, or one that marks a whole field, can be an operand.
- Operators (17.16.3.3), highest precedence first, same level left to right: unary
-;^;*,/,%;+,-; then=,<>,<,<=,>,>=. A comparison gives 1 for true and 0 for false, and any non-zero value tests true. - Functions (17.16.3.4):
ABS(x),AND(x,y),AVERAGE(list),COUNT(list),DEFINED(x)(1 if x is well formed),FALSE(0),INT(x),MAX(list),MIN(list),MOD(x,y)(x - ny with the sign of x and a magnitude below that of y; y = 0 is a diagnostic),NOT(x),OR(x,y),PRODUCT(list),ROUND(x,y)(to floor(y) decimals; a negative y rounds the integer part to a power of ten),SIGN(x),SUM(list),TRUE(1). Names are not case-sensitive and may be followed by white space. Arguments are separated by the list separator of the document settings (w:listSeparator, 17.15.1.56), comma or semicolon. - Table cells (17.16.3.5): cells are named by column letter and row number,
A1at the top left; letters are not case-sensitive. A reference is a cell, a comma-separated set, or a rangeA1:B2; a row or column named alone (B:B,1:1) is the whole row or column, however many are added later. Inside a cell, a list function can takeABOVE,BELOW,LEFTorRIGHTin any case: the range stops at a cell that is blank or not numeric, except that a blank first cell counts as 0. A cell of another table is reached through a bookmark on that table:(Table1 A1:A3).
Formatting switches (17.16.4)
The result of a field is formatted by a switch, or by a default. There are three kinds. A switch that does not fit the type of the result has no effect.
Date and time: \@ picture (17.16.4.1)
Without the switch a date or time is formatted in an implementation-defined way. The picture is
a series of items; the language comes from w:lang (17.3.2.20) on the run holding the field
code. A repeated letter makes the longer item, and the longest groups are formed first, in the
direction of the run's text.
| Item | Result |
|---|---|
yy, yyyy, y; YY, YYYY, Y | Year as 2 or 4 digits (y is 2 digits). Gregorian by default; the \s and \h switches and the bb, bbbb and Thai items change the calendar (Y forms to Thai Buddhist Era). |
BB, BBBB | Year as 2 or 4 digits; Gregorian, also following \s and \h. |
bb, bbbb | Year as 2 or 4 digits: the Gregorian year when w:lang is zh-TW, zh-CN, zh-HK, zh-SG or zh-MO, otherwise the Thai Buddhist Era year. They also change other items. |
M, MM, MMM, MMMM | Month: number without a zero, number with a zero, abbreviated name, full name (by language). |
d, dd, ddd, dddd; D, DD, DDD, DDDD | d and D: day (of the week or of the month, as the standard words it) as a number without a leading zero; dd: with a leading zero; DD: the day of the month as two digits; ddd, DDD: abbreviated day-of-week name; dddd, DDDD: full day-of-week name. The standard does not say how d chooses between week day and month day. |
h, hh, H, HH, m, mm, s, ss, am/pm | Used in the examples of 17.16.4.1 (h:mm am/pm, HH:mm:ss) but not in the picture-item table of the standard; the text gives 5:28 PM for h:mm am/pm and 17:28 for HH:mm. |
e, ee, n, nn | Japanese Emperor Era year, without or with a leading zero. |
E, EE | E: ja-JP gives the Emperor Era year, zh-TW the Taiwanese year, other languages the four-digit Gregorian year using ja-JP; EE: the four-digit Gregorian year, in ja-JP, zh-TW, zh-CN, zh-HK, zh-SG or zh-MO form, otherwise in ja-JP. |
g, gg, ggg, G, GG | Era name: abbreviated Emperor Era (g, gg, G), full name (ggg, GG); ko-KR gives the Korean Tangun era (gg, ggg, GG), zh-TW the Taiwanese era; g and G show nothing for ko-KR and zh-TW. |
aaa, w, W | Abbreviated day or month name in ja-JP and ko-KR (W also in the Chinese languages); otherwise the item is shown as literal text. |
A, O | Day of the month, or month, as a number without a leading zero in Japanese numerals. |
Thai items ว, วว, ววว, วววว; ด, ดด, ดดด, ดดดด; ปป, ปปปป | Day number (1 or 2 digits), day of week (abbreviated, full), month (number, 2-digit number, abbreviated, full name) in the Thai Buddhist Era calendar and Thai numerals; the last pair is the Gregorian year in Thai numerals. |
| any other character | Copied to the result (colon, hyphen, asterisk, slash, space are usual). |
'text' | Text copied to the result. |
`name` | The number of the preceding item numbered by a caption or a SEQ field of the same name, in Arabic numerals. |
Examples from the standard (US English, 3 January 2006, 17:28:34): M/d/yyyy gives 1/3/2006;
dddd, MMMM dd, yyyy gives Tuesday, January 03, 2006; d-MMM-yy gives 3-Jan-06;
'Today is 'HH:mm:ss gives Today is 17:28:34.
Numeric: \# picture (17.16.4.2)
Without the switch a number has no leading spaces or trailing fractional zeros, a minus sign if negative, and no radix point if whole.
| Item | Result |
|---|---|
0 | A required digit position, filled with 0 (=4+5 \# 00.00 gives 09.00). |
# | A required position, filled with a space; extra fractional digits are rounded off (=9+6 \# $### gives "$ 15"). |
x | Drops the digits to the left of it; to the right of the radix point it rounds to that place (=111053+111439 \# x## gives 492; =1/8 \# 0.00x gives 0.125; =3/4 \# .x gives .8). |
., , | The radix point and the three-digit group separator, as the locale writes them. |
- | A minus sign for a negative result, a space for a positive one or 0. |
+ | A plus sign for a positive result, a minus for a negative one, a space for 0. |
other character, 'text', `name` | Copied; the last form is the number of the preceding item numbered by a caption or SEQ of that name. |
pos;neg and pos;neg;zero | Different pictures for positive, negative and zero results; zero uses the positive picture when there are two. |
General: \* argument (17.16.4.3)
For a numeric result (17.16.4.3.1) the argument selects a number format. Each matches a value of
ST_NumberFormat (17.18.59; the numbering page lists them): Arabic = decimal;
ALPHABETIC = upperLetter and alphabetic = lowerLetter (26 is subtracted until the value is 26 or less, and the letter is repeated once per subtraction, so 54 is BBB);
Roman / roman = upperRoman / lowerRoman; Ordinal = ordinal (32nd);
CardText = cardinalText; OrdText = ordinalText; DollarText = dollarText (integer part as text, then and nn/100);
Hex = hex (upper-case); ArabicDash = numberInDash;
AIUEO = aiueoFullWidth; IROHA = irohaFullWidth; ARABICABJAD = arabicAbjad; ARABICALPHA = arabicAlpha;
BAHTTEXT = bahtText; CHOSUNG = chosung; GANADA = ganada;
CIRCLENUM = decimalEnclosedCircle (1 to 20 only, else as Arabic); DBCHAR = decimalFullWidth; SBCHAR = decimalHalfWidth;
GB1 = decimalEnclosedFullstop; GB2 = decimalEnclosedParen; GB3 = decimalEnclosedCircleChinese; GB4 = ideographEnclosedCircle;
HEBREW1 = hebrew1; HEBREW2 = hebrew2; HINDIARABIC = hindiNumbers; HINDICARDTEXT = hindiCounting; HINDILETTER1 = hindiVowels; HINDILETTER2 = hindiConsonants;
THAIARABIC = thaiNumbers; THAICARDTEXT = thaiCounting; THAILETTER = thaiLetters; VIETCARDTEXT = vietnameseCounting;
ZODIAC1 = ideographTraditional; ZODIAC2 = ideographZodiac; ZODIAC3 = ideographZodiacTraditional.
Four depend on the language: CHINESENUM1 = chineseCounting (zh-CN) or taiwaneseCounting (zh-TW);
CHINESENUM2 = chineseLegalSimplified or ideographLegalTraditional; CHINESENUM3 = chineseCountingThousand or taiwaneseCountingThousand;
DBNUM1 = ideographDigital (ja-JP) or koreanDigital (ko-KR); DBNUM2 = japaneseCounting or koreanCounting;
DBNUM3 = japaneseLegal or koreanLegal; DBNUM4 = japaneseDigitalTenThousand, koreanDigital2 or taiwaneseDigital (zh-TW);
KANJINUM1, KANJINUM2, KANJINUM3 choose among koreanDigital, ideographDigital, chineseCounting, taiwaneseCounting; koreanCounting, chineseCountingThousand, chineseLegalSimplified, ideographLegalTraditional; koreanLegal, japaneseLegal, chineseCountingThousand, taiwaneseCountingThousand, by language (ko-KR, ja-JP, zh-CN, zh-TW in that order).
For a string result (17.16.4.3.2): Caps (first letter of each word), FirstCap (first letter of the first word), Lower, Upper.
For any result (17.16.4.3.3), which apply after a field update (when that happens is outside the standard):
CHARFORMAT gives the whole result the formatting of the run holding the first character of the field code;
MERGEFORMAT keeps the existing result's run and paragraph structure: delete the old result text, put the new text into the existing runs and paragraphs, delete runs left over, add runs for overflow.
A format set directly on the first run of the field-type name wins over a general format switch, so \* Lower on small-capitals text is ignored.
Viewer: reading never evaluates a formula or applies a switch; it shows the stored result, so these
rules matter to a program that updates fields. The viewer substitutes only PAGE and NUMPAGES
and does not apply their switches.
Divergence from spec
- The standard says the default of
w:historyon a hyperlink is false (17.16.22). python-docx declares it withdefault=True(docx/oxml/text/hyperlink.py, line 28), so a file without the attribute reads as true there. - Apache POI models only
w:fldSimpleas a field run (XWPFFieldRunwraps aCTSimpleFieldand givesgetFieldInstruction); complex fields come through as plain runs withw:fldCharandw:instrTextinside. - docx4j finds complex fields by counting
beginandendcharacters (ComplexFieldLocator.java, lines 45 to 59), and lists a paragraph once however many fields start in it. ItsFieldsPreprocessorworks one paragraph at a time, so fields that run over several paragraphs, such asTOC, are not safe to canonicalise. - Word treats
w:ffDataas required on a form field: without it the field is invalid and cannot be used or edited, where the standard says the default values apply (MS-OI29500 2.1.518 a). Word also ignores and does not round-tripw:labelandw:tabIndexinsidew:ffData(2.1.518 b), and limits thew:formatvalue of a text form field to 64 characters (2.1.520 a). - The examples of the standard are not all valid against its own schema. The
w:ddListexample in 17.16.28 writesw:defaultbeforew:result, butCT_FFDDListordersresult,default,listEntry; thew:statusTextexample in 17.16.31 is insidew:ddList, while the schema puts it directly inw:ffData. The 17.16.7 example callsw:size w:val="20""ten points", which is right (half-points). The viewer reads the schema order and also finds the children in the other order.
Compared for this page: 17.16.1 to 17.16.34, 17.18.25 to 17.18.29, 17.13.6.1 and 17.13.6.2;
ffData children against wml.xsd (CT_FFData and its types, Part 4 Transitional); the formula
and formatting clauses 17.16.2 to 17.16.4.3.3 read in full. Not compared: python-docx, POI and docx4j
form-field handling (not read); the 72 field definitions of 17.16.5 are listed by name only.