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.
In the Select Table dialog box, click the Customers table, and then click OK.
Click OK.
Click Next.
In step 4, click More Items to display the Insert Merge Fields dialog box. Select the ID field, click Insert, and then click Close.
Save the document as C:\Doc1.docx, and then exit Word.
Microsoft Word 2003 or Microsoft Word 2002
Start a new document. On the Tools menu, click Letters and Mailings, and then select Mail Merge Wizard.
In Step 1 of the Mail Merge Wizard, click Next to accept Form Letters as the document type.
In Step 2, click Next to use the current document for the form letters.
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:
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.
Save the document as "C:\Doc1.doc" and quit Word.
Microsoft Word 97, 2000
Start a new document and, on the Tools menu, click Mail Merge.
In the Mail Merge Helper dialog, click Create, click Form Letters, and then click Active Window.
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.
Click Edit Main Document when prompted. The Mail Merge toolbar appears.
Click the Insert Merge Field button on the Mail Merge toolbar, and then choose CustomerID.
Save the document as "C:\Doc1.doc," and then quit Word.
Start a new project in Microsoft Visual Basic.
On the Project menu, click References. Select the object Library for your version of Microsoft Word, and then click OK.
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
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.
For additional information on automating Microsoft Word to perform a mail merge, click the article number below
to view the article in the Microsoft Knowledge Base: