Microsoft KB Archive/211802

= WD2000: How to Use Microsoft Query to Merge Two Excel Files =

Article ID: 211802

Article Last Modified on 12/6/2000

-

APPLIES TO


 * Microsoft Word 2000 Standard Edition

-



This article was previously published under Q211802





SUMMARY
This article describes how to use Microsoft Query to merge two Microsoft Excel 2000 files. You may want to do this so that you can combine related information into a single file and use it in a Microsoft Word mail merge.



MORE INFORMATION
Merging two Excel files allows you to combine related information into a single file.

For more information about creating a mail merge data source in Microsoft Excel, click Microsoft Excel Help on the Help menu, type work with a mail merge data source in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

Example: Merging Two Excel Files
One file can contain a list of ID numbers, first names, last names, and positions, and another file can contain a list of ID numbers, addresses, and salaries.

If the ID numbers in both files are the same, you can link the two files by using the similar (or common) ID field. This allows you to create a mail merge that contains the ID numbers, first names, last names, addresses, positions, and salaries. For the files to be combined inside Microsoft Query, the files must be in the correct format in Excel. Each column of information needs a heading name (do not use spaces, quotation marks, apostrophes, or hyphens in the heading names).

In these two sample Excel files, File1, cell A1 contains the word "IDNumber," cell B1 contains the word "Firstname," cell C1 contains "Lastname," and cell D1 contains "Position." In File2, cell A1 contains the word "IDNumber," cell B1 contains "Address," and cell C1 contains "Salary."

File1:   A1: IDNumber B1: Firstname C1: Lastname   D1: Position A2: 1       B2: James     C2: Jones      D2: Clerk A3: 2       B3: Harry     C3: Anderson  D3: Administration File2:   A1: IDNumber   B1: Address        C1:  Salary A2: 1         B2: 333 3rd Ave.   C2: $20,000 A3: 2         B3: 444 4th St.    C3: $25,000 The IDNumber column in File1 should be similar to the IDNumber column in File2. That is, the column heading should be the same, and the information in File1 should correspond to the information in File2. That is, IDNumber 1 should represent the same employee in both sheets. That is, James Jones (IDNumber 1 in File1) should live at 333 3rd Ave (IDNumber 1 in File 2). When you connect the two files using Microsoft Query, you can retrieve all of the information about James Jones into a single data source.

Modifying the Files in Excel
After the files have been created, name the ranges of information in each sheet. To do this, follow these steps:
 * 1) Start Microsoft Excel.
 * 2) Open the first workbook, and then select the sheet that contains the information that you want to use in the mail merge. Select the portion of the file containing the text.
 * 3) On the Insert menu, point to Name, and then click Define.
 * 4) Type a name, click OK, and then save the file.
 * 5) Repeat steps 1-4 for the second file. (Be sure to save the files in the same directory.)
 * 6) Quit Excel.

After modifying the files in Excel, you can start the mail merge in Word.

Starting the Mail Merge
To start the mail merge, follow these steps:
 * 1) Open Word.
 * 2) On the File menu, click New, and then click OK.
 * 3) On the Tools menu, click Mail Merge.
 * 4) Click Create, click Form Letters, click Active Window, and then click Close.

Now you're ready to create the data source.

Creating a Microsoft Query Data Source
To create the Microsoft Query data source, follow these steps:
 * 1) In the Word Mail Merge Helper, click Get Data, click Create Data Source, and then click MS Query.
 * 2) With New Data Source selected, click to select the Use the Query Wizard to create/edit queries check box, and then click OK.
 * 3) Type any name for your data source.
 * 4) Under Select a driver, select Microsoft Excel Driver (*.xls).
 * 5) Click Connect.
 * 6) Select the Database Version (for example, Excel 97-2000), and then click Select Workbook.
 * 7) Locate and select the first Excel file, and then click OK until you return to the Choose Data Source dialog box.

Creating the Query
To create the query, follow these steps:
 * 1) Your newly created data source should be selected in the Choose Data Source dialog box. If it is not selected, select it, and then click OK.
 * 2) In the Query Wizard - Choose Columns dialog box, under Available Tables and Columns, double-click the named range that appears.

This reveals your column titles.
 * 1) One at a time, select the column names that you want to use to create the mail merge data source, and then click the > button.

NOTE: You must select a field that both sheets have in common -- in this case IDNumber.
 * 1) Click Next three times (to ignore filtering and sorting).
 * 2) Click the View Data or edit query in Microsoft Query option, and then click Finish.

Microsoft Query starts, and the information from the first Excel file is available.

Adding the Second File and Joining the Data in Microsoft Query
To join the data in Microsoft Query, follow these steps:
 * 1) In Microsoft Query, click Add Tables on the Table menu.
 * 2) At the bottom of the Add Tables dialog box, next to Workbook, select the other Excel file that you want to use in the merge.

NOTE: This file must be located in the same directory as the file you added in the Query Wizard.
 * 1) Click Add, and then click Close.
 * 2) In the first Excel table, click the field that you want to join (for example, the IDNumber field), and then drag the pointer to the similar field in the second Excel table.

A join line should appear.

NOTE: Some field names may appear in the query pane (the big white rectangle under the tables). You can use these fields, or you can delete each one by positioning the insertion point over the field name until a DOWN ARROW is displayed. To delete the field, select the column, and then press DELETE.

Selecting the Fields That You Want to Use in the Mail Merge
To select the fields that you want to use in the mail merge, follow these steps:
 * 1) In the first table, click the first field name that you want to use.
 * 2) Drag the field name to the query pane.
 * 3) Repeat steps 1 and 2 for each field that you want to use.
 * 4) On the File menu, click Return Data to Microsoft Word.

Word then prompts you with the option to Edit Your Main Document.

Creating the Mail Merge Main Document
To create the mail merge main document, follow these steps:
 * 1) When you receive the "Word found no merge fields in your main document..." message, click Edit Your Main Document.

The merge fields from both Excel files are now available under the Insert Merge Field button on your mail merge toolbar.
 * 1) To insert the merge fields in your document, click the Insert Merge Field button on the mail merge toolbar.
 * 2) On the Tools menu, click Mail Merge.
 * 3) Click the Merge button.
 * 4) Click the Merge button to merge to a new document.

The merged data should include information from both Excel tables.

