WD2000: How to Format a Combination of Five-Digit and Nine-Digit Postal Codes (284705)



The information in this article applies to:

  • Microsoft Word 2000

This article was previously published under Q284705
For a Microsoft Word 2002 version of this article, see 284718.

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 Word mail merge to format the result to contain a hyphen in a nine-digit postal code.

The field contained in the "More Information" section of this article provides a consistent output, regardless of whether the data source (address list) contains or does not contain hyphens in the PostalCode field.

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

98052

If your data source contains a nine-digit postal code with or without a hyphen (dash), the resulting postal code appears as:

98052-6399

MORE INFORMATION

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 ({ }), use the key combination CTRL+F9. Using the "{" and "}" keys on the keyboard does not work.
  • 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 the name of the postal code field is PostalCode.
  • Be sure that each record contains a postal code; otherwise, an error message similar to the following appears for that record
    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 PostalCode field.
  • 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 surround the hyphens.
  • This code may not work if the PostalCode 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 PostalCode fields as text (rather than as numbers) in Microsoft Access. Then postal codes beginning with zero will be available in Word.

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 PostalCode field, the field construction shown 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 begin with a zero are evaluated the same as numbers that do not begin with a zero. For example, "01234" is evaluated by Word as equal to "1234".
The PostalCode field is placed in the main document with a hyphen, so that the formula can operate on it. Its absolute value is taken, which converts the number to a positive value. This new number is compared to the literal postal code. If the numbers differ, it is a nine-digit code, and this is printed with formatting to keep any leading zeros. If the calculated number is different from the literal postal code, the postal code is printed as is.

Modification Type:MajorLast Reviewed:2/11/2001
Keywords:kbfield kbinfo kbmerge KB284705