This book has touched on fields several times, but only in relation to their role in other Word features. You've seen fields used to insert dates and page numbers, to number equations, and a few other things. So far, however, the book hasn't delved into fields and how to use them to automate document content.
A number of tasks that required fields in Word 2003 and earlier can now be performed more efficiently and elegantly with content controls (contained in the Controls section of the Developer tab; see Chapter 24, “Creating Custom Forms”). If you work with complex documents in compatibility mode, you are much more likely to encounter fields than when working in the .docx format.
Moreover, a number of linking tasks that require the use of fields in a compatibility-mode document can be performed in ways that are more robust (harder to break) in the XML-based Word .docx format, so when you insert links you're less likely to see field codes than before. Instead, much is done behind the scenes with XML technology.
Yet some Word 2013 features—dates, page numbers, mail merge, tables of contents, indexes, and some others—still do rely on fields. This book covers a number of the field-dependent features elsewhere. This chapter's mission is not to redundantly cover those other features. Rather, it is to provide a strong background and foundation that helps you thoroughly understand the relationship between fields and specific features, and how you can insert fields manually to make a document easier to use and update. Equipped with this information, you'll be in a solid position to take advantage of the power of fields when needed, to choose between legacy and newer approaches when the option presents itself, and to deal with the occasional challenges that inevitably creep in.
Field codes are special sets of instructions in Word documents. They sometimes tell Word to display the current date, the number of pages in a document or a document section, an index, a table of contents, the contents of all or of part of a linked document, or the file name and folder where the current document is stored. Field instructions (called switches) also tell Word how to display the specified information.
Word enables you to update most fields. When the source information for a field changes, you can update the field so it displays the latest information. This enables you to turn documents into more dynamic information containers, rather than static documents that always contain the same information. To update a field, click it and press F9. Other update methods are available as well, as you will see later in this chapter.
You can format fields just like regular text. You can apply font, paragraph, and other formatting to field results so that they blend in with your document. Sometimes you don't even know information is in a field code until you look more carefully. As you will see, however, for the person working on the document, knowing that something is a field is critically important.
Many fields can be unlinked easily and turned into permanent content that doesn't change. This enables you to create a snapshot, rather than dynamic content. This means that you can use the convenience of a field to insert today's date, but then lock that date in so that when you look at a letter years from now it will have the original date, rather than some date in 2018. That way you'll know when the letter was written, but if you repurpose a copy of that letter, you can unlock the date field so it can be updated.
As suggested earlier, sometimes fields are inserted automatically as part of some other process, such that you aren't even specifically aware that fields are involved. For example, in the Insert tab's Text group, click the Insert Date and Time field button, as shown in Figure 23.1.
In response, the Date and Time dialog box springs to life. If you choose a format and click OK, you'll get just the date and/or time in the format selected, and not a field. If you enable the Update automatically option indicated in Figure 23.2 before clicking OK, Word instead inserts a DATE field.
The word update has been used several times because it's impossible to talk about fields otherwise. Updating a field simply means forcing Word to reevaluate that field in light of any changes that might have occurred either to it directly or to source materials on which it is based.
To update a field, select it and do one of the following:
Update a field when it has been created or edited manually or when the source or circumstances have changed. Circumstances can include a wide variety of things that affect context. You will learn more about them throughout this chapter.
When you insert a date field using the Insert Date and Time button, it initially looks like a simple date (depending on your settings—more about that later in this section). However, if you select the date or use the mouse to hover over it, you see something very different, as shown in Figure 23.3. The gray container shown is a special means by which Word displays some fields and a number of other features so that you realize you're not dealing with ordinary text. Not all fields display this way; in fact, most do not. Because date fields have been a frequent source of confusion and problems in the past, however, Microsoft has chosen to display them this way in recent versions of Word.
Most other fields, such as tables of contents and indexes, don't display in containers. How or whether those fields display in a special way is up to you.
To change the way fields are displayed, choose File Options Advanced. Scroll down to Show document content, and note the setting for Field shading, shown in Figure 23.4.
In addition to using special containers for some fields, Word can shade fields so that you're aware of their presence in your document. The options are as follows:
The When selected option provides a nice balance of information and aesthetics, as shown in Figure 23.5. With Field shading set to display When selected, Word shades only the field holding the insertion point. If Always had been selected, all four fields would have been shaded.
CAUTION
With Field shading set to Never, it's possible to edit the interior of a field's displayed results without realizing it. This frequently happens with dates and tables of contents. The next time you update fields (which can even happen automatically when you print a document), however, your careful edits get zapped! If you have a tendency to do this, consider changing to the Always setting for field shading.
The other major option shown in Figure 23.4 is Show field codes instead of their values. Enable this option to display the underlying field code either for diagnostic reasons or so you can manually edit it. If you don't see what you expect to see in an inserted field, toggling a field code display on can help solve the mystery. The same text shown in Figure 23.5 is shown again in Figure 23.6 with field codes displayed. Notice that for the Last Updated line, there is an extra field, LASTSAVEDBY, that should be deleted.
The Show field codes instead of their values option shown in Figure 23.4 displays all field codes, not just the one in question. To see just the one in question, right-click it and choose Toggle Field Codes, as shown in Figure 23.7. If you select text holding multiple fields before right-clicking, this command will toggle all fields in the selection. Toggling one or several field codes can be helpful when you're trying to understand what's happening with a particular set of related fields. If you are familiar with the syntax of the field, you can also edit the field directly, rather than editing it with the Field dialog box or using other indirect methods (such as Insert Text Date and Time or Page Number).
NOTE
After creating or editing a field code, don't forget to update it. Otherwise, it will either not display a result at all or not display the new result.
When you need to display all the fields as codes, rather than having to drill into the File Options Advanced settings, it's much simpler and quicker to press Alt+F9. Other field-related shortcut keys incorporating F9 or F11, as listed in Table 23.1, perform such actions as updating fields.
In Table 23.1, notice the last two commands. Unknown by most users, these aren't used nearly enough, but they are exceedingly useful when you want to control whether a date field, a link to bookmarked text, or some other field can be updated. Select the field (in fact, if it's the very next field in the document you can press F11 to go right to it), and press Ctrl+F11. Press F9 to try to update it. It doesn't work! Press Ctrl+Shift+F11 to unlock it. Now F9 works.
TIP
If you want a good test field for trying out such behavior, insert a Time field, using an option that includes displaying seconds. That way you'll never have to wait more than a second before the field is out of date.
Recall from the beginning of this chapter that some field operations have been replaced by XML relational linking—such as when you insert a link to a picture. In Word 97–2003 compatibility mode, you still get an INCLUDEPICTURE field, but not in a Word 2013 .docx file. What's interesting is that the F9 key works to update the non-field picture links, but Ctrl+F11 does not work to lock them. Worse, once you've updated such a link by pressing F9, Ctrl+Z won't undo the update.
In addition to the Toggle Field Codes option shown in Figure 23.7, there are other contextual commands. Note that these appear only when a field is selected:
If a block of text is contained in the selection, Word displays the default Field dialog box rather than the setup for any particular field. If you proceed nonetheless, any field you insert will replace all the selected text rather than affecting a single field (unless you have Typing replaces selected text turned off, as described in Chapter 4, “Zapping Word's Top Annoyances”).
In the Field dialog box, you choose the options you want and Word automatically builds the necessary field code for you. Here's how to use the dialog box to create a field:
NOTE
Word has additional field codes that aren't accessible through the Field dialog box. Information on these is beyond the scope of this book, but you can find out more on the Microsoft Developer Network (MSDN) website.
If you're just exploring the Field dialog box, you could click the second item under Field Names and observe the right side of the dialog box. As you choose different fields, the array of Field properties and Field options changes. The offerings on the right are determined by the type of field and its capabilities. For example, some are designed to work with bookmarks, and a list is offered accordingly. Others work with dates, and corresponding date formats are shown from which you can choose.
Note that many fields have an option to Preserve formatting during updates, seen in the lower right in Figure 23.8. When you select this option Word adds a * MERGEFORMAT switch to the field code. For example, a DATE field code with this option might look like this:
{ DATE @ “MMMM d, yyyy” * MERGEFORMAT }
The * MERGEFORMAT switch tells Word to preserve any formatting you might have applied so that in a subsequent update your formatting will be preserved. That is the theory, and it works quite well for some fields, including dates. For example, if you make that date bold, it will stay bold in subsequent updates.
However, it doesn't necessarily work for REF fields to bookmarks for which the contents change, or for a number of other fields. Consider what happens if the bookmarked passage is:
[Now is the time for all good men to come to the aid of their country.]
You can use a REF field to reproduce that text elsewhere in the document. If the bookmark's name is “countrymen,” the field code { REF countrymen * MERGEFORMAT } could reproduce the original text elsewhere in the document. Suppose you add italics to the word good in the reproduced text, but it's not italicized in the original. With the * MERGEFORMAT switch in place, updating the field will leave “good” alone. Without the switch, “good” reverts to its original form.
Now consider what will happen if you completely rewrite the original text so that it no longer contains the word good at all, but still uses the same bookmark. In this case, Word arbitrarily formats the seventh word in italics when you update the REF field! Keep this odd behavior in mind before you rely heavily on the Preserve formatting option. It's best when formatting field results to use only formatting that applies to the entire field result, rather than to individual words.
Looking back at Figure 23.8, with Categories set to (All) and Field Names set to SaveDate, notice the Field Codes button in the lower-left corner. If you click it, the button face now says Hide Codes. The right part of the dialog box now says Advanced field properties, and includes a Field codes text box with the field information in it, as shown in Figure 23.9.
The syntax model shown below the Field code text box provides information how to manually edit the field. For some fields, the Options button next to Hide Codes is grayed out as unavailable. That doesn't mean that no options are possible for this field code; you can just click Hide codes to redisplay the previous options for the field in the dialog box. When the Options button is active, clicking it opens the Field Options dialog box shown in Figure 23.10.
Click a listed option on either the General Switches or Field Specific Switches tab, review the Description that appears near the bottom of the dialog box to determine whether you've selected the appropriate option, and then click Add to Field. Repeat as needed, and then click OK to return to the field dialog box. Click OK again if you are finished editing the field.
The next section explains more about field switches, regardless of whether you want to enter them in the Field dialog box or display a field code in the document and edit it manually.
Some fields are very simple. Their only syntax is the keywords by which they're identified. The field { BIBLIOGRAPHY } for example, uses no parameters whatsoever. Other fields can be rather complex, requiring both parameters and switches to achieve the desired effect. Furthermore, for some reason known only to Microsoft, some fields require quotes around literal arguments, whereas others do not.
Although syntax can vary substantially for various field types, a number of general rules apply. The general form for the field statement is as follows:
{ keyword [instructions] [switches] }
NOTE
Technically, Word distinguishes slightly between what appears in the middle brackets and shorter field-specific switches. But because the Field Options dialog box calls them all switches, this chapter follows suit.
Note that anything inside brackets [] is optional. Anything not in brackets is required.
The keyword can be any the field types (including field types created by third parties for Word add-ins; those fields can be used only if the corresponding add-in has been installed). The field character pair, which you can insert by pressing Ctrl+F9, can also be used to enclose just a bookmark reference, such as {duedate}. In such cases, the REF keyword is implied, and is almost equivalent to {REF duedate}. I say almost equivalent, because the two fields behave differently sometimes. For example, if you apply formatting to a REF field code's displayed results, Word adds a * MERGEFORMAT switch to the field. If you apply formatting to a plain bookmark field that doesn't use REF, Word will not add the switch. If you right-click a REF bookmark field and choose Edit Field, you'll get the full services of the REF field, replete with a list of bookmarks and switches. If you right-click a plain bookmark field, you'll get no services at all.
Anytime the first word in a field statement isn't one of the recognized keywords, Word tries to interpret the field as a bookmark. When manually inserting a field code, if you mistype or if the bookmark name isn't otherwise defined, when you try updating the resulting field Word informs you that something is wrong by yelling “Error! Bookmark not defined.” If you see this error message, it means that you've mistyped a keyword or a bookmark or that a bookmark has been deleted.
The actual ordering of arguments and switches matters sometimes, but not always. As a general rule, switches that affect the entire field precede any arguments. A switch that affects a particular parameter usually appears immediately after the parameter it affects.
You can use three types of general switches. These can be used with a number of different fields and they affect how the results of fields are displayed:
Text format switches use the following syntax:
* format
They are used to affect how certain text will appear. The format options can be any of the keywords that follow.
NOTE
Each switch only works with certain fields. For example, adding the * firstcap switch to the AUTHOR field does not change the capping of the username information used by that field.
Numeric format switches, sometimes called numeric picture switches, have the following syntax:
# format
These are quite versatile, enabling you to format numbers in a wide variety of ways.
The # parameter is sometimes a source of confusion. That's because in addition to being used as the overall numeric format switch (#), it can also be used as a specification or argument within the switch—for example, {=6765.44 # $#,.##}. Note that the # in this field is the numeric switch. The additional # parameters specify how the number should appear. In this case, the noted field would produce $6,765.44. The $ means that the result will be preceded by $. The next # is a placeholder for any significant digits to the left of the decimal point. The comma means that commas will be used every three places to the left. The remaining .## reserves two significant digits to the right of the decimal point. The number 6765.444444 would therefore be displayed as $6,765.44.
The numeral 0 is used as a placeholder to guarantee the same order of precision in numbers. This is useful when you have multiple formula fields that ordinarily might yield different precisions. For example, it's generally unacceptable to list the same statistic for different individuals using different precisions, such as in the following table:
Courtney | 4.216 |
Ryan | 3.7 |
Colleen | 4.4 |
Matt | 4.7231 |
Suppose these numbers were all exact, and the result of calculations performed by Word. By using the same numeric switch in all the calculation fields (for example, {=59/14 #0.000}), you ensure that all the results are shown to thousandths, with a leading 0 for any that are less than 1 (for example, 0.412 instead of .412).
When x is used at the left edge of any other arguments, Word truncates additional digits that don't fit in the reserved space. For example, {=42753 # x#.00} results in 53.00 because x# reserves only two places to the left of the decimal point. When used to the right of the decimal point, x serves the same purpose as #.
The decimal point is used in conjunction with # and 0 placeholders to specify the precision of the displayed result.
The comma is used to insert commas to separate three-digit groups (thousands) to the left of the decimal point. If you want only commas and numbers, the comma and any other number placeholder (except for x) will work. You don't need {=55000 # ###,###} to get 55,000. A simple {=45000 # #,} works just as well.
Include the plus sign to force the positive or negative display of the number. No sign is displayed for 0. Consider the following example:
The temperature in Frostbite Falls, Minnesota, was {=sum(weathertable[A1]) # +#}.
If the A1 cell in a table bookmarked as weathertable says −57, it will display as −57. If it's 37 in the table, then the field result will display +37, and the residents will be putting on their shorts and T-shirts.
The semicolon is used to specify different formats for positive, negative, and 0. If a single semicolon is used, the picture format to the left controls the display of positive and zero values, and the picture format to the right of the semicolon controls the display of negative numbers. If two semicolons are used, the first argument specifies positive format, the second specifies negative format, and the third specifies zero format.
For example, consider the switch # *+###*;^+###^;=0.#=. The numbers 65, −7, and 0 would display respectively as *+65*, ^−7^, and =0.0=. Why would you do that? Because you can!
Single quotes are used to insert literal text into the number display when the text contains characters otherwise reserved for use as part of the format switch. When you do this, the entire numeric format must be enclosed in double quotes.
Enclosing reserved characters in single quotes prevents the single-quoted text from being interpreted as a numeric switch argument. For example,
{ =8.9 # “Please add #.0% sales tax”}
displays as
Please add 8.9% sales ta
Recall that x is a switch argument. For this to work properly, the x in tax must be enclosed in single quotes:
{ =8.9 # “Please add #.0% sales ta‘x’” }
You can, of course, enclose the entire literal text in single quotes. Minimum quoting is used here to demonstrate why it's necessary. Alternatively, move to a state that doesn't have a sales tax.
Date-time picture switches have the following syntax:
@ format
They enable you to format dates in a variety of ways, as detailed in Table 23.2.
You can specify a desired default format using the Date and Time dialog box. Choose Insert Text Date and Time. Highlight the format you want to use for the default and click Default. Choose Yes to confirm. Back in the Date and Time dialog box, click OK to insert a date, or Cancel if you're simply changing the default. The default was changed when you confirmed changing the default; canceling doesn't cancel the change.
Characters | Effect |
AM/PM or am/pm | AM and PM or am and pm, respectively |
A/P or a/p | A and P or a and p, respectively |
D | Date, with no leading zero |
Dd | Date, with leading zero |
Ddd | Abbreviated name of the day of the week (SUN, MON, etc.) |
Dddd | Full name of the day of the week (Sunday, Monday, you know the rest) |
H | Hour, 12-hour format with no leading zero |
Hh | Hour, 12-hour format with leading zero |
H | Hour, 24-hour format with no leading zero |
HH | Hour, 24-hour format with leading zero |
M | Numeric month, no leading zero |
MM | Numeric month, leading zero |
MMM | First three letters of month (Jan, Feb, etc.) |
MMMM | Full name of month (March, April, etc.) |
M | Minutes, no leading zero |
Mm | Minutes, leading zero |
S | Seconds, no leading zero |
Ss | Seconds, leading zero |
y or yy | Year, two-digit format (07, 08, etc.) |
Yyyy | Year, four-digit format (2013, 2014) |
Date formats use key letters to represent parts of dates and times, as shown in Table 23.2. These key letters affect only the component that is displayed, not the capitalization. You must add the appropriate * switch to achieve different forms of capitalization. The field {date @ “MMM”} produces Aug (an uppercase M produces months, while a lowercase m produces minutes). You would need to add * upper to get AUG. See the following section for other exciting ideas.
Some of the switches can be combined. For example, * CHARFORMAT or * MERGEFORMAT can be combined with any other format. The field {quote * CHARFORMAT * ordtext “9”} produces ninth (in italics because the q in quote is in italics). However, none of the numeric arguments can be combined with case arguments, except for alphabetic.
The Field dialog box separates the fields into several categories. In this section we will look at each category, the fields they contain, and what those fields are used for.
Word provides six different date and time fields. Of the six, note that only the Date field itself, which provides the current date, uses the special document control container.
All of these except for EditTime can be used as either Time or Date fields depending on the picture switches applied. Note also that for each of the Time and Date fields, when you display the dialog box, as shown in Figure 23.11, the top format is shown (such as 11/21/2012), even if the selected field name is Time.
If you select the field and then just click OK, accepting the default date format, that may not be the format displayed by the field. In the case of Date, you will get the default date format (which you can change as shown earlier). In the case of Time, you will get just the time. For CreateDate, PrintDate, and SaveDate, however, you will get a combination of the default date and the time. Therefore, if you want one of those three—and you generally want the time as well—you don't need to build a formatting switch. It's already been done for you, the dialog box's display notwithstanding.
The document automation fields are used mostly in building automated forms. The fields can be used to perform logical operations and to allow the user to run a macro, send special instructions to the printer, or go to a bookmark. Six fields are provided:
Document information fields are taken from the Document Properties stored behind the scenes for a file. You can view some of them with the File Info choice, and they display at the right side of the screen that appears. Click Show All properties below the list of Properties to see the full list of available properties.
The Document Information category contains a variety of document properties that you can insert into the document. With the exception of the DOCPROPERTIES field, each of the field codes can be inserted directly into the document to display the corresponding datum about the document. Note that some fields such as COMMENTS, SUBJECT, and TITLE generally must be entered by someone (using the document panel or Properties dialog box). A number of these are automatically created by Word, such as FILESIZE, NUMCHARS, NUMPAGES, and NUMWORDS. The 14 document information fields are as follows:
This group is used to perform calculations, manipulate characters, construct equations, and display symbols. Though the formula (=) field will still attract a few diehard users in Word 2013, for the most part the functionality offered by this group is for backward compatibility. Four fields are provided:
The index and tables fields are used to build and maintain tables of contents, indexes, and tables of authorities. The seven fields in this category are as follows:
These fields are used to link content into Word documents from external files as well as from other parts of the current Word document. Fields are provided in those cases both for backward compatibility and to allow for different methods if other options are preferred. The 11 fields in this category are as follows:
The fields included in the Mail Merge group are used in constructing mail merge documents of the type shown when you choose Start Mail Merge in the Mailing dialog box. See Chapter 22, “Data Documents and Mail Merge,” for the full scoop.
Some of the fields in the Mail Merge category are also shown under Document Automation. The 14 Mail Merge fields are as follows:
Numbering fields are used to insert certain kinds of numbering into your documents. The 10 field codes in the Numbering category are as follows:
User information corresponds to the User Name and Initials fields in File Options General, as well as to the Mailing Address field found in File Options Advanced General. The three user information fields are as follows:
In this chapter you've learned about field codes: what they are, how to insert them, and why you might want to. You've learned about numerous fields available in Word and how to use them. You should now be able to do the following: