Microsoft KB Archive/284718

= How to format a combination of five-digit and nine-digit postal codes in Word =

Article ID: 284718

Article Last Modified on 1/29/2007

-

APPLIES TO


 * Microsoft Office Word 2003
 * Microsoft Word 2002 Standard Edition

-



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 \# &quot;####'-'#####&quot; } ) } <> { MERGEFIELD PostalCode }¶

{ MERGEFIELD PostalCode \# &quot;#####'-'####&quot; } { 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 &quot;{&quot; and &quot;}&quot; 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.

where  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 &quot;PostalCode&quot; 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.</li> Note that single quotation marks enclose the hyphens.</li> If you create your data document by using the Mail Merge Helper in Word, the default field name for the postal code is &quot;Postal_Code.&quot; Substitute &quot;Postal_Code&quot; for &quot;PostalCode&quot; when you create the merge field.</li> 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 &quot;01234&quot;), Word interprets it as &quot;1234&quot;. 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.</li></ul>

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, &quot;01234&quot; is evaluated by Word as equal to &quot;1234&quot;.

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

Additional query words: OfficeKBHowTo postal codes us u.s. post office Zipcode Code 4Plus 4 Plus four format switch merge printmerge mailmerge print mail inf zip WD2003 WD2002

Keywords: kbdta kbfield kbhowtomaster kbmerge KB284718

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.