Microsoft KB Archive/258523

= How to determine the number of merged records before executing a mail merge =

Article ID: 258523

Article Last Modified on 4/3/2007

-

APPLIES TO


 * Microsoft Office Word 2007
 * Microsoft Office Word 2003
 * Microsoft Word 2002 Standard Edition
 * Microsoft Word 2000 Standard Edition
 * Microsoft Word 97 Standard Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q258523



SUMMARY
This article describes how to get the number of records that are to be merged in a Word Mail Merge Document before performing the mail merge.



MORE INFORMATION
You can determine the number of records that are to be merged in a Word Mail Merge Document before performing the mail merge by setting the ActiveRecord property of the DataSource object to wdLastRecord, and then querying the ActiveRecord property for the record number. The following sample demonstrates how to do this:  Create a new mail merge document for form letters. The steps below walk you through how to accomplish this in the different versions of Microsoft Word.

Microsoft Office Word 2007  Open a new document. On the Mailings tab, click Start Mail Merge, and then click Step by Step Mail Merge Wizard. In step 1 of the Mail Merge Wizard, click Next to accept Letters as the document type. In step 2 of the Mail Merge Wizard, click Next to use the current document for the form letters. In step 3 of the Mail Merge Wizard, click Browse, and then do the following:  In the Select Data Source dialog box, locate the sample Microsoft Access database Northwind.mdb, and then click Open.</li> In the Select Table dialog box, click the Customers table, and then click OK.</li> Click OK.</li> Click Next.</li></ol> </li> </li> In step 4, click More Items to display the Insert Merge Fields dialog box. Select the ID field, click Insert, and then click Close.</li> Save the document as C:\Doc1.docx, and then exit Word.</li></ol>

Microsoft Word 2003 or Microsoft Word 2002 <ol style="list-style-type: lower-alpha;"> Start a new document. On the Tools menu, click Letters and Mailings, and then select Mail Merge Wizard.</li> In Step 1 of the Mail Merge Wizard, click Next to accept Form Letters as the document type.</li> In Step 2, click Next to use the current document for the form letters.</li> In Step 3, click Browse. In the Select Data Source dialog box, browse to the Sample Microsoft Access database Northwind.mdb, and then click Open. In the Select Table dialog box that appears, choose the Customers table, and then click OK. Click OK in the Mail Merge Recipients dialog box that appears.

NOTE: The default location for sample Microsoft Access 2002 databases is:

C:\Program Files\Microsoft Office\Office10\Samples.

</li> In step 3 of the Mail Merge Wizard, click Next.</li> In step 4 of the Mail Merge wizard, click More Items to display the Insert Merge Fields dialog box. Select the CustomerID field, click Insert, and then click Close.</li> Save the document as "C:\Doc1.doc" and quit Word.</li></ol>

Microsoft Word 97, 2000 <ol style="list-style-type: lower-alpha;"> Start a new document and, on the Tools menu, click Mail Merge.</li> In the Mail Merge Helper dialog, click Create, click Form Letters, and then click Active Window.</li> <li>Click Get Data in the Mail Merge Helper dialog box, and then select Open Data Source. Select the file type for MS Access Databases in the Open Data Source dialog box, browse to the sample Microsoft Access database Northwind.mdb, and then click Open. Select the Customers table, and then click OK.

NOTE: The default location for sample Microsoft Access databases is:

C:\Program Files\Microsoft Office\Office\Samples.

</li> <li>Click Edit Main Document when prompted. The Mail Merge toolbar appears.</li> <li>Click the Insert Merge Field button on the Mail Merge toolbar, and then choose CustomerID.</li> <li>Save the document as "C:\Doc1.doc," and then quit Word.</li></ol> </li> <li>Start a new project in Microsoft Visual Basic.</li> <li>On the Project menu, click References. Select the object Library for your version of Microsoft Word, and then click OK.</li> <li> Add a CommandButton to Form1, and add the following code to the click event of that button: Private Sub Command1_Click

Dim oApp As Word.Application Dim oDoc As Word.Document 'Start a new document in Word Set oApp = New Word.Application Set oDoc = oApp.Documents.Open("C:\doc1.doc") 'Make Word visible oApp.Visible = True With oDoc.MailMerge .DataSource.ActiveRecord = wdLastRecord 'Display the number of records that are to be merged Ret = MsgBox(.DataSource.ActiveRecord & " records will be " & _                   "merged. Click Yes to continue or No to quit.", _                    vbYesNo + VbMsgBoxSetForeground)

If Ret = vbYes Then 'Proceed with mail merge .Execute Else 'Quit Word without saving any changes oApp.Quit False End If  End With

End Sub </li> <li>Press F5 to run the program, and then click Command1.

Results: The Mail Merge Document is opened, and the number of records that are to be merged is displayed. Click Yes to continue with the mail merge. Or, click No to cancel the mail merge and exit Word.</li></ol>

<div class="references_section">