MS Word Field Codes

A few notes, useful as an aide memoir (for Les)….
This is not a tutorial!

alt + f9 toggles view codes on and off

f9 updates fields but I suggest re-write a macro to update all fields and save this as an updated key press f9:

remember ”  ‘  ” at the start of a line is the equivalent  of “rem” (remark).

Sub UpdateAllFields()

‘ UpdateAllFields Macro
‘ Macro recorded 09/02/2005 by Les

Dim oStory As Range
Dim oField As Field
For Each oStory In ActiveDocument.StoryRanges
For Each oField In oStory.Fields
oField.Update
Next oField
Next oStory
End Sub
————————————————————-

«Title» «FirstName» «LastName»
«Address1»
«Address2» «Address3»
«Town» «County»
«PostCode»

These field codes actually look like:

{ MERGEFIELD PostCode }

————————————-

code to multiply a2 by c2 and show answer or blank if zero
{ =product(a2,c2) \# #,##0.00;(#,##0.00); }

code to add up column of numbers d2 to d10 and show answer or blank if zero
{ =sum(d2:d10) \# #,##0.00;(#,##0.00); }

code to calculate vat 20% and show answer or blank if zero
{ =d11*20/100 \# #,##0.00;(#,##0.00); }

code to add two cells d11 and d12 and show answer with £ sign or blank if zero
{ =sum(d11,d12) \# £#,##0.00;(£#,##0.00); }

 

How to type fields in Word

The { } field delimiters can not be typed using the braces characters on the keyboard. They are special Word characters that have to be inserted as a pair using Ctrl-F9, or Insert => Field… or Insert => Cross Reference… or some other special field insertion command. If you are using Ctrl-F9 you can either press Ctrl-F9 first and then type your field or type the field code first, select it, and press Ctrl-F9. Once you are done with the field, press F9 to update the field and display the result.

How to copy a field from a newsgroup or email message into Word:

If you are using a typed field display from a newsgroup or email message first make sure that the field isn’t broken up by any line breaks or paragraph breaks. (Lines that are wrapped automatically by Word are fine but turn on Show All to make sure there are no special characters at the end of the line.) Select the text inside a matched pair of braces and press Ctrl-F9. Word will insert its field delimiters around the text. Delete the typed braces.

For instance, with the field:
{ = { REF “Number1” } * { REF “Number2” } }
first select
REF “Number1”
and press Ctrl-F9, then select
REF “Number2”
and press Ctrl-F9, then delete all of the typed braces and select everything from the = sign to the end of the second field and press Ctrl-F9 again.

Then press F9 to update the field and display the result.

If you press Alt-F9 (view field codes) you’ll see something that looks very much like what you started with except the field delimiters look like bold braces and the field will probably be shaded.  Press Alt-F9 again to go back to viewing the field result.

 

The Function and Shortcut Keys that manipulate fields

Windows Keys

What

Macintosh Keys

F9

Update/Refresh Selected Field

F9

Shift-F9

Display/Hide Field Codes (selected field) *

Shift-F9

Alt-F9

Display/Hide All Field Codes (all fields in document) **

Opt-F9

Ctrl-F9

Create Empty Field
(Insert { }Braces)

Cmd-F9

Ctrl-Shift-F9
or
Ctrl-6

Unlink Selected Field (delete all field coding and replace with field result) – turn field into text

Cmd-Shift-F9

F11

Go to (Select) Next Field

F11

Shift-F11

Go to (Select) Previous Field

Shift-F11

Ctrl-F11
or
Ctrl-3

Lock Field (prevent manual or automatic update)

Cmd-F11
or
Cmd-3

Ctrl-Shift-F11
or
Ctrl-4

Unlock Field (allow updating)

Cmd-Shift-F11
or
Cmd-4

Alt-IF

Insert Field (Dialog Box)
(Alt-I followed by F)

Opt-I,F

Alt-Shift-D

Insert Date Field ***

Ctrl-Shift-D

* It is possible to have fields inside of fields (nested fields). If this is the case, the Shift-F9 Toggle will not show all the field codes, only the outermost layer. To see all the field codes, use Alt-F9 instead.

** Alt-F9 is the same as checking / un-checking Field Codes in Tools => Options => View. Unlike a field code toggled using Shift-F9, if you update a field while all field codes are being displayed, the new field result will still be hidden and you will see the field codes.

*** Probably not the field you want to insert, though.

 

The DATE field and its variations.

The easy way (but probably wrong way for what you want) to put a date in your document is Insert => Date or Alt-Shift-D.

If you don’t check “Update Automatically” it is the same as typing the date yourself. If you do check “Update Automatically” it will update when you print (if you have the setting under printer options as “Update Fields” which is the default). You can manually force an update by putting your insertion point in the date and pressing the [ F9] key.

If you want to put a date in a template that updates to the current date when a document is created based on the template, or want to change the format or do other things with the date field, you want to use Insert => Field => Date and Time instead. Using the options here, you can either pick a format or type your own characters (called a picture) for the format. The options for the type of date include:

{ DATE } – The date you are looking at the document. Always today (although it may not show on screen as today until you update the field).
{ CREATEDATE } – The date the document was created (or saved using Save As).
{ PRINTDATE } – The date the document was last printed.
{ SAVEDATE } – The date the document was last saved.

The above are the field codes that will be inserted for you using Insert => Field => Date and Time without using any options. A “\*MERGEFORMAT” switch is automatically inserted if you check “preserve field format” and means leave formatted the same way the field is formatted. Generally, you do not want to check this box or add this switch. If you want one of these formatting switches, you probably want “\*CHARFORMAT” instead.  This switch formats the field result however the first non-blank character within the field is formatted.

If you choose options, they can include the following “pictures:”

 Picture

 Displayed Date

 \@MMMM d, yyyy

 August 1, 2000

 \@MMM dd, yyyy

 Aug 01, 2000

 \@MM/dd/yy

 08/01/00

\@dddd, MMMM d

 Tuesday, August 1

 \@ddd, MMM. d, yyyy

Tue., Aug. 1, 2000

 \@MM/dd/yy hh:mm:ss am/pm

 08/01/00 10:36:12 PM

example: { CREATEDATE \@ “MMM dd, yyyy” \*MERGEFORMAT }

The word “picture” in the above table indicates a guide to Word on how you want your field to display and print. (Normally called a “mask” in other programming but Microsoft decided to call this a picture for its own reasons.) If you don’t like the pictures you are offered, pick the one that is closest to what you want and then modify it in the Insert Field dialog box (or in the codes themselves using Toggle Field Codes).

 

Calculated Fields – Conditional Fields

A calculated field is one that does some kind of calculation and shows you the result. It starts with an equals sign:

= 1 + 3 – 1 }

will show a result of 3. Calculations are performed in much the same way as in algebraic expressions.

Field

Result

= 1 + 3 – 1 }

3

= 1 + 3 – 1 * 4 }

0

= (1 + 3 – 1) * 4 }

12

See the help files for more information on the rules for what gets calculated first.

Conditional Fields – The IF field

The structure of an {IF} field is: {IF expression “truetext” “falsetext”}Expression means a statement that will be either True or False. {IF 1=1 “One does equal one!” “This one doesn’t equal that one!”} Trivial? How about: {IF {REF “Gender”} = “Male” “He” “She”}. (Download the Gender Toolbar template to see how this works in real documents.) You may have noticed that we have one field inside of another field here! Yes, fields can use other fields for parts of their structure.

Practice: A footer entry that only appears on the last page?

How would you put the document name and path in the footer of your document to appear only on the last page of the document?

The first thought is “I have put a condition on this. I need a conditional Field! – an {IF} field!  In this case, there are two page numbering fields that can be used and compared.

{PAGE} = {NUMPAGES}

The first field is the field to display the current page number. The second is to display the total number of pages in the document. Normally, these are used in “Page {PAGE} of {NUMPAGES}.” The only time this statement will be true is if this is the last page in the document. We have our expression.

We want the document’s name with its path. The field for a document’s name can be found in the Document Information category: the {NAME} field. An optional switch for this field is “\p” so what we want is the field {NAME \p}. We can add something else to our {IF} field to print on other pages if we want; how about the Page X of Y noted above? So, our conditional field looks like:

{IF {PAGE} = {NUMPAGES{NAME \p}” “Page {PAGE} of {NUMPAGES}” }

On the last page, this will print the full document name. In a four-page document, on page three, it will print “Page 3 of 4.”

Nested {IF} Fields

One {IF} can be used inside of another one to give logical branches and twists.

{IF {PAGE} = 1 “This is the first page.” “{IF {PAGE} < {NUMPAGES} “Page {PAGE} of {NUMPAGES}” “{NAME \p}” }” }

This is called “nesting.” The nested IF field is used if the first expression is false. If you think this is complex, wait until we tackle date fields!

Date Fields

Here is an example of a calculated date field written in response to the request:

I need to create a fiscal year field in Word that accomplishes in Word what the following statement accomplished in English:

If today’s month and day are less than or equal to June 30, display the current year, else display current year + 1.

{ IF { DATE \@ “MM” } < 07 { DATE \@ “yyyy” } { = { DATE \@ “yyyy” }+1 \# “0000” } }

If today were January 31, 2001, the following expression using that field for the year would display: “June 30, 2001” where the year is the field and the rest of the date is regular typed text. If today were July 1, 2001, the same expression would display as  “June 30, 2002.” This might be used in a phrase such as “the end of this fiscal year, June 30, 2001.”

This uses the test IF field together with date fields and an equation field to display the result if the result is a different year. The formats for the dates are set using date pictures. However, if the result is a different (calculated) year, then you need to use a number picture. In this case, I started out with a two-digit year and was using the picture 00. With a four-digit year, the picture becomes irrelevant.

The field shown above handles the year part of the date. It would have to be combined with an additional DATE field to get the month and day. Note that the field above might work better with a CREATEDATE field instead of the DATE field. With the CREATEDATE field and the additional field for the month and day, we get:

{ CREATEDATE \@ “MMMM d”}, { IF { CREATEDATE \@ “MM” } < 07 { CREATEDATE \@ “yyyy” } { = { CREATEDATE \@ “yyyy” }+1 \# “0000” } }

 

Here’s a head-spinner! The following is an example of a field that gives you the date two weeks from the day a document is created, in regular written English format:

Creation date: July 9, 2001 – desired field result = July 23, 2001
Creation date: July 23, 2001 – desired field result = August 6, 2001
Creation date: December 20, 2001 – desired field result = January 3, 2002
Creation date: February 27, 2003 – desired field result = March 12, 2003
Creation date: February 27, 2004 – desired field date result = March 11, 2004

This next example uses a Fill-in field to supply additional data to a mail merge. It lets you supply an interview time for letters to be sent out to candidates held in a database.

  1. Open the Tools menu and select Mail Merge…
  2. Click on the [Create] button and choose Form Letters…
  3. In the dialog box which now appears, click on [New Main Document]
  4. Next click on the [Get Data] button and choose Open Data Source…
  5. Open the file D:\User\advanced.xls by changing drives to D: and opening folder User – you’ll also have to change the list to show Files of type: Excel (*.xls)
  6. Accept Entire Spreadsheet as the cell range – click on OK or press <Return>
  7. Press <Return> again to [Edit Main Document]

You now have to type in your invitation to interview letter:

  1. Type Dear then, using the [Insert Merge Field] button, add Title and Surname – don’t forget to type a space to separate these two fields
  2. Press <Return> twice then type You are invited for interview at
  3. Now open the Insert menu and choose Field…
  4. Under Categories: click on Mail Merge and from the Field Names: select Fill-in
  5. In the box provided, click after the word FILLIN then type "Enter the interview time"

Note: You can set a default value for this field by adding a \d “default_value switch.

  1. Click on [OK] twice, leaving the interview time value empty
  2. Finish off the letter by typing a full-stop, and adding Yours sincerely on a new line

Your letter is now complete – a real one would be much longer. It’s now time to mail merge:

  1. Click on the button labelled Merge… on the Mail Merge toolbar
  2. There’s no need to merge all 390 records here, in the To: box type 3
  3. Click on [Merge] then for each candidate type in the time: eg 9:30 , 10:00 and 10:30

The mail merge will now be carried out, with the appropriate times inserted in the three letters.

Leave a Reply

Your email address will not be published. Required fields are marked *