Training: MS Word IF {} field code introduction
Created on 2010-10-01 by Sona Gabrielyan
Switzernet
Before you start, please make sure you followed training sessions “Introduction to MS Word field codes”, MS Word Styles.
The field codes are used in MS Word document when we merge data from an Excel file into the document, or when we deal with data that might change. In this last case, it means that if we create a template text containing variables (field codes), so the presentation of the final text becomes dynamic. The text will change automatically, when the values of variables are changed.
In the MS Word document, we see the results of the field codes. The field codes by themselves are usually invisible. To visualize a particular field code, we select it, right click on the selection and choose Toggle Field Codes from the popup menu. Otherwise, to switch between the mode of display of all field codes of the document and the results, we can use the keyboard shortcut ALT+F9.
The Field codes appear between curly brackets ({ }). A new field code can be inserted by using keyboard shortcut CTRL+F9.
The purpose of this exercise is to learn how to use {if} field code.
As the field codes are invisible, we use the blue square brackets to mark the place in the document where the field codes are used. We use pink for values of parameters.
To set a parameter we use {set name value} field code, where name is the name we give to the parameter and value is the value (see Figure 1).
Figure 1. Definitions of parameters
To display a parameter, we write the parameter name between the curly brackets (see Figure 2). Note that he curly brackets of field codes must be called using CTRL+F9 and not by just writing {} using the keyboard.
Figure 2. Display of parameters
{if} field compares two values and then inserts the text appropriate to the result of the comparison. The syntax is the following:
{ if Expression1 Operator Expression2 TrueText FalseText }
Using the {if} field, we compose “composed definitions” from the previously defined parameters (see Figure 3). Here Expression1 is “pay“; Operator is “>“; Expression2 is “35“, TrueText is “private“; FalseText is “collective“. If there is no space in FalseText or TrueText (in our example, “private” and “collective”) the quotes (“”) can be omitted.
Figure 3. Composed definitions
We check the value of parameter pay using {if} and we fill the text parameter accordingly. If the value of pay is > 35, the word “private” will appear in final text. If we set the pay to a number less than 35, we get “collective” in the text. In our example, pay is set to 45. See the value of text parameter in Figure 4
Figure 4. Value of text parameter
Using the parameters which we defined via field codes, we can compose a dynamic text for example for an announcement. To include parameters into the text, we insert curled brackets using CTRL+9 shortcut. Between “{ }” we write the parameter name.
Figure 5. Text of announcement containing parameters
If we
set pay to 25 (Figure 1) and update the text of the
announcement, we obtain the text presented in Figure 6.
Figure 6. Text of announcement after modification of the value of parameter ‘pay’
Note that every time when you change data in a field, you must select it, right click on the selection and click on Update field.
Follow the training session and produce the same text. Additionally, create three new parameters: fname, lname, training_date. Set the value of fname as your first name, lname as your last name, and training_date to the current date. At the end of the text of announcement, add the following sentence “Created by xxx yyy, on zzz”. Where xxx is the value of the fname, yyy the value of the lname and zzz is the value of the training_date.
Make prinscreens corresponding to Figure 5 and Figure 6: showing the text of announcement with field codes and the text of announcement with results. Make a prinscreen showing the “Styles and Formatting” pane of your document. Make a prinscreen of the list of bookmarks of your document.
Open a new doc file and copy the four printscreens into the new doc file (pictures corresponding to Figure 5, Figure 6, your “Styles and Formatting” pane and bookmarks). This doc file with prinscreens must be uploaded on the training session web site, according to the guidelines.
MS Word Field Reference: http://office.microsoft.com/en-us/word-help/CH006104723.aspx?CTT=97
MS Word Styles: http://switzernet.com/2/support/091026-training-MS-Word-style/
Training sessions: http://www.unappel.ch/2/support/100722-training-employees/i/
* * *