Microsoft KB Archive/211303

= HOW TO: Use Mail Merge to Create a List Sorted by Category in Word 2000 =

Article ID: 211303

Article Last Modified on 8/29/2007

-

APPLIES TO


 * Microsoft Word 2000 Standard Edition

-



This article was previously published under Q211303



IN THIS TASK
SUMMARY
 * How to Set Up the Data File
 * How to Set Up the Main Document
 * How to Force Each New Category to a New Page
 * How to Format the Key Field
 * How This Process Works

REFERENCES



SUMMARY
You can use the Mail Merge feature in Microsoft Word to create a list of data sorted and separated by a given category. This article provides instructions and a sample that you can use to create such a list.

How to Set Up the Data File
Sort your data file so that all records with the same value for the key field category (the field upon which you base the sort) appear together, as shown in the following sample data file. The following sample list is sorted by the City field (City is the key field in this example):   City        Employee      Sales Atlanta    Smith        $3,000 Atlanta    Gates       $50,000 Atlanta    Henderson   $10,000 Houston    Jones        $8,000 Houston    Kelley       $9,000 Houston    Peterson         $0 back to the top

How to Set Up the Main Document
NOTE:  designates a paragraph mark in the following example. To type a paragraph mark, press ENTER.

To set up your main document as a catalog, follow these steps:
 * 1) Open a new blank document, and then on the Tools menu, click Mail Merge.
 * 2) In the Mail Merge Helper, click Create, and then click Catalog.
 * 3) Click New Main Document when prompted.
 * 4) In the Mail Merge Helper, click Get Data, and then click Open Data Source to attach the data file (use the sample file that you created in the "How to Set Up the Data File" section of this article).
 * 5) Insert the following fields to compare the contents of each key field record with the contents of the next key field record to determine whether the key field contents change from one data record to the next.

NOTE: This example uses the sample data from the "How to Set up the Data File" section of this article. To insert the field braces, press CTRL+F9.

{ IF { MERGESEQ } = "1" "{ MERGEFIELD City }" "" }

{ SET Place1 { MERGEFIELD City }}

{ If { Place2 } <> { Place1 }"

{ MERGEFIELD City }



{ MERGEFIELD Employee }{ MERGEFIELD Sales }" "{ MERGEFIELD Employee }{ MERGEFIELD Sales }" }{ SET Place2 { MERGEFIELD City }}

The fields laid out in this example produce a catalog listing on the same page, as follows:

Atlanta

Smith $3,000

Gates $50,000

Henderson $10,000

Houston

Jones $8,000

Kelley $9,000

Peterson $0

back to the top

How to Force Each New Category to a New Page
The key field in this example is { MERGEFIELD City }. When the value of City changes in the data file to a different city, a new page is added to the merged results, and the merge operation continues at the top of the next page. To insert the field braces, press CTRL+F9.

{ If { MERGESEQ } = "1" "{ MERGEFIELD City }" ""}

{ SET Place1 { MERGEFIELD City }}

{ If { Place2 } <> { Place1 }"

Page Break

{ MERGEFIELD City }



{ MERGEFIELD Employee }{ MERGEFIELD Sales }" "{ MERGEFIELD Employee }{ MERGEFIELD Sales }" }{ SET Place2 { MERGEFIELD City }}<ENTER>

NOTE: To insert a page break, either press CTRL+ENTER, or click Break on the Insert menu, select Page Break, and then click OK.

The fields laid out in this example produce a catalog listing on separate pages as follows:

Atlanta

Smith $3,000

Gates $50,000

Henderson $10,000

Page Break-

Houston

Jones $8,000

Kelley $9,000

Peterson $0

back to the top

How to Format the Key Field
The key field in this example is {MERGEFIELD City}. To format the results of the {MERGEFIELD City} as all capital letters, you can insert the formatting switch of \* Upper. To insert the field braces, press CTRL+F9.

{ If { MERGESEQ } = "1" "{ MERGEFIELD City \* Upper }" "" }<ENTER>

{ SET Place1 { MERGEFIELD City }}<ENTER>

{ IF { Place2 } <> { Place1 }"<ENTER>

{ MERGEFIELD City \* Upper }<ENTER>

<ENTER>

{ MERGEFIELD Employee }{ MERGEFIELD Sales }" "{ MERGEFIELD Employee }{ MERGEFIELD Sales }" }{ SET Place2 { MERGEFIELD City }}<ENTER>

The fields laid out in this example produce a catalog listing on the same page, with the city appearing in all uppercase letters, as follows:

ATLANTA

Smith $3,000

Gates $50,000

Henderson $10,000

HOUSTON

Jones $8,000

Kelley $9,000

Peterson $0

NOTE: You can apply different formatting to the key field, { MERGEFIELD City }, by selecting the entire field (including the field braces { }), and formatting the field to your preferences. To format the field, click Font on the Format menu.

back to the top

How This Process Works
The first IF field in these examples inserts the city name and a return character (<ENTER>) only for the very first record in the mail merge. For all subsequent records, this IF field inserts nothing (""). It recognizes the first mail-merge record by comparing the MERGESEQ field (which returns the sequence number for the current record) to the numeral "1".

The second IF field inserts a return character (or a page break, if it forces each city to a new page), the city name, two more return characters, the employee name, and the sales amount (in that order), only if the IF field determines that the current record includes a city name that is different from the preceding record. If it determines that the current record includes a city name that is the same as the preceding record, it inserts only the next employee name and sales amount.

This IF field recognizes the new city name by comparing the text of the bookmarks specified in the two SET fields. The text of the bookmark "Place1" is always equal to the current record's City field, and the text of the bookmark "Place2" is equal to the preceding record's City field.

back to the top

<div class="references_section">