How to format a combination of five-digit and nine-digit postal codes in Word (284718)



The information in this article applies to:

  • Microsoft Office Word 2003
  • Microsoft Word 2002

This article was previously published under Q284718
For a Microsoft Word 97 version of this article, see 196671.
For a Microsoft Word 2000 version of this article, see 284705.

SUMMARY

When your data source (address list) contains a mixture of five-digit and nine-digit postal codes, you can use a field in your Microsoft Word mail-merge operation to format the postal codes.

This article describes a way to ensure consistent output, regardless of whether the data source (address list) contains hyphens in the postal code field.

For example, if your data source contains five-digit postal codes, the resulting postal code is displayed as follows:

98052

However, if your data source contains a nine-digit postal code, with or without a hyphen, the resulting postal code appears as follows:

98052-6399

back to the top

How to create a merge field for postal codes

In Word, use the following field:
{ IF { =ABS ( { MERGEFIELD PostalCode \# "####'-'#####" } ) } <> { MERGEFIELD PostalCode }
{ MERGEFIELD PostalCode \# "#####'-'####" } { MERGEFIELD PostalCode } }

NOTES:
  • You cannot copy and paste this field code from this article into your Word mail-merge main document. If you do, the text is not resolved as a field, and your Word mail-merge main document will not contain a MERGEFIELD.
  • To create the special field code braces ({ }), press CTRL+F9. If you use the "{" and "}" keys on the keyboard, the field code is not correctly implemented.
  • To create the paragraph mark, press ENTER. To view the paragraph marks in your Word document, click Show/Hide on the standard toolbar.
  • Be sure that each record contains a postal code; otherwise, you receive an error message similar to the following
    A field calculation error occurred in record n.
    where n is the record of your data source that does not contain a value in the postal code field.
  • You can change the merge field name from "PostalCode" to something else, but nothing else in the formula can be changed.
  • The formatting of the first merge field has four number signs followed by five number signs. This appears incorrect but is necessary for the calculation that takes place.
  • Note that single quotation marks enclose the hyphens.
  • If you create your data document by using the Mail Merge Helper in Word, the default field name for the postal code is "Postal_Code." Substitute "Postal_Code" for "PostalCode" when you create the merge field.
  • This code may not work if the postal code field is from a Microsoft Access data source and if that field is formatted as a number. If the postal code starts with a 0 (for example "01234"), Word interprets it as "1234". To work around this behavior, format the postal code fields as text (rather than as numbers) in Microsoft Access. Then postal codes that start with zero will be available in Word.
back to the top

How the field works

Word does not include fields that count the number of characters in another field. Therefore, to determine the number of digits in the postal code field, the field construction provided earlier in this article takes advantage of two facts:
  • When you format a numeric field to include a hyphen, the hyphen is treated by other fields as a subtraction sign.
  • Numbers that start with a zero are evaluated in the same way as numbers that do not start with a zero. For example, "01234" is evaluated by Word as equal to "1234".
The postal code field is placed in the main document with a hyphen, so that the formula can operate on it. The formula takes the field's absolute value, which converts the number to a positive value, and then compares this new number to the literal postal code. If the numbers differ, this means that the code is nine-digits, and it is printed with formatting to retain any leading zeros. If the calculated number is different from the literal postal code, the postal code is printed as is.

back to the top

Modification Type:MajorLast Reviewed:9/25/2006
Keywords:kbdta kbfield kbHOWTOmaster kbmerge KB284718