Microsoft KB Archive/302665

= WD2000: How to Work Around Duplicate Names in Mail Merge Data =

Article ID: 302665

Article Last Modified on 8/2/2001

-

APPLIES TO


 * Microsoft Word 2000 Standard Edition

-



This article was previously published under Q302665



SUMMARY
In some cases, a data source for a mail merge duplicates each contact's name on several rows.

For example, a data source might be a list of medical patients and doctors that repeats each doctor's name for each patient of that doctor. As a result, if a doctor has 20 patients, the doctor's name appears 20 times.

When you set up a mail merge in Microsoft Word 2000, a data source that is sorted this way presents a problem if you only want one letter for each contact. That is because a mail merge in Word generates a new letter for every instance of each contact's name (taking the previous example, if a doctor has 20 patients, the merge produces 20 letters to that same doctor).

As a workaround, this article describes how to use mail merge to create letters that are sorted by a category that you define, instead of the sorting used in the data file.

It is recommended that you practice by using the example in this article before you try this with your own data source and mail-merge document. This procedure can be confusing, and you must understand how it works before applying it in a real world environment.



Setting Up the Data File
Group together all records with the same contact name (the DOCTOR field is the contact name in this example), as shown in the following sample data file: DOCTOR         PATIENT Dr. John       Jim Dr. John       Caroline Dr. John       Michael Dr. Hook       Valerie Dr. Hook       Reggie Dr. Hook       Warren Dr. Jekyl      Laura Dr. Jekyl      Tom Dr. Jekyl      Isabelle For additional information about setting up mail merge data, click the article number below to view the article in the Microsoft Knowledge Base:

212322 WD2000: How to Design and Set Up Mail Merge Data

Setting Up the Main Document
For this procedure to work, you must choose the Catalog document type when you set up your main document. To set up your main document as a catalog, follow these steps:  In a new blank document, click Mail Merge on the Tools menu. In the Mail Merge Helper, click Create, and then click Catalog. Click New Main Document when prompted. 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 &quot;Setting Up the Data File&quot; section earlier in this article). When the dialog box appears, select Edit Main Document. Using the following example as a guide, insert fields to compare the DOCTOR field in each row with the DOCTOR field in the previous row, and to insert appropriate text based on the results of the comparisons.

NOTE: The brace characters {} in the example specifically denote field codes. Typing brace characters on the keyboard does not work. To insert the field codes, press CTRL+F9.

The ¶ characters represent paragraph marks (also called carriage returns or hard returns). To type a paragraph mark, press ENTER.

To insert the page break, press CTRL+ENTER.

{IF {MERGESEQ} = &quot;1&quot; &quot;Dear {MERGEFIELD Doctor},¶

¶

Here is a list of your patients:¶

&quot; &quot;&quot;}{SET Doctor1 &quot;{MERGEFIELD Doctor}&quot;}{IF {Doctor2} <> {Doctor1} &quot;¶

¶

Sincerely,¶

Your Name¶

Page Break

Dear {MERGEFIELD Doctor},¶

¶

Here is a list of your patients:¶

¶

{MERGEFIELD Patient}&quot; &quot;¶

{MERGEFIELD Patient}&quot;}{SET Doctor2 &quot;{MERGEFIELD Doctor}&quot;}

The fields laid out in this example produce letters on separate pages, as follows:

Dear Dr. John,

Here is a list of your patients:

Jim

Caroline

Michael

Sincerely,

Your Name

Page Break

Dear Dr. Hook,

Here is a list of your patients:

Valerie

Reggie

Warren

Sincerely,

Your Name

Page Break

Dear Dr. Jekyl,

Here is a list of your patients:

Laura

Tom

Isabelle

NOTE: You might notice that the last letter does not contain a signature. For an explanation of this limitation, see &quot;The Example Explained&quot; section later in this article.

Understanding IF Fields
To understand how this process works, you must understand the structure of an IF field.

An IF field has the following five parts:
 * The value that you are comparing
 * The operator, such as equals (=), does not equal (<>), is less than (<), or is greater than (>)
 * The value to which you are comparing (in quotation marks)
 * The text to insert if the comparison resolves to true (in quotation marks)
 * The text to insert if the comparison resolves to false (in quotation marks)

A basic IF field looks similar to the following:

{IF Comparison value = &quot;Compare to value&quot; &quot;Insert this text if the comparison resolves to true&quot; &quot;Insert this text if the comparison resolves to false&quot;}

With the exception of the operator, each part of the IF can contain other fields, such as a MERGEFIELD or MERGESEQ field. This is called a &quot;nested&quot; field.

In addition, the true and false text can contain formatted text, carriage returns, tabs, page breaks, graphics, tables, and so on, as long as all of the text and graphics are contained within the quotation marks. You therefore can produce highly detailed letters by using the method in this article.

The Example Explained
The first IF field in the example from &quot;Setting Up the Main Document&quot; inserts a salutation and a letter body only for the first record in the data file. Thereafter, the IF field inserts nothing (&quot;&quot;). It recognizes the first record by comparing the MERGESEQ field (which returns the sequence number for the current record) to the numeral &quot;1.&quot;

The second IF field inserts only the next patient name if the doctor name in the current record is the same as the doctor name in the previous record.

If the doctor name in the current record is different from the doctor name in the previous record, the second IF field starts a new letter by inserting a signature, a page break, and a new salutation.

This is why the last letter in this merge example does not include a signature: Word adds the signature and page break to start a new letter when the doctor name changes. Because there is no doctor name after the last record, this does not occur.

The second IF field recognizes the new doctor name by comparing the value of the two SET fields. &quot;Doctor1&quot; is always equal to the current record's doctor name, and &quot;Doctor2&quot; is equal to the previous record's doctor name.

When you use this workaround, all text for the letters that you are producing must be within the quotation marks in the IF fields. This way, the text appears only if the data meets the conditions that you define (for example, when the doctor name changes).

If you place text outside the IF fields in your main document, this text appears in the merge document one time for each record in the data source, rather than one time for each new contact name. Taking the example in this article, therefore, the text would appear one time for each patient, instead of one time for each doctor.

<div class="references_section">