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

Article translations Article translations
Article ID: 258523 - View products that this article applies to.
This article was previously published under Q258523
Expand all | Collapse all

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:
  1. 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
    1. Open a new document.
    2. On the Mailings tab, click Start Mail Merge, and then click Step by Step Mail Merge Wizard.
    3. In step 1 of the Mail Merge Wizard, click Next to accept Letters as the document type.
    4. In step 2 of the Mail Merge Wizard, click Next to use the current document for the form letters.
    5. In step 3 of the Mail Merge Wizard, click Browse, and then do the following:
      1. In the Select Data Source dialog box, locate the sample Microsoft Access database Northwind.mdb, and then click Open.
      2. In the Select Table dialog box, click the Customers table, and then click OK.
      3. Click OK.
      4. Click Next.
    6. In step 4, click More Items to display the Insert Merge Fields dialog box. Select the ID field, click Insert, and then click Close.
    7. Save the document as C:\Doc1.docx, and then exit Word.
    Microsoft Word 2003 or Microsoft Word 2002
    1. Start a new document. On the Tools menu, click Letters and Mailings, and then select Mail Merge Wizard.
    2. In Step 1 of the Mail Merge Wizard, click Next to accept Form Letters as the document type.
    3. In Step 2, click Next to use the current document for the form letters.
    4. 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.
    5. In step 3 of the Mail Merge Wizard, click Next.
    6. 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.
    7. Save the document as "C:\Doc1.doc" and quit Word.
    Microsoft Word 97, 2000
    1. Start a new document and, on the Tools menu, click Mail Merge.
    2. In the Mail Merge Helper dialog, click Create, click Form Letters, and then click Active Window.
    3. 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.
    4. Click Edit Main Document when prompted. The Mail Merge toolbar appears.
    5. Click the Insert Merge Field button on the Mail Merge toolbar, and then choose CustomerID.
    6. Save the document as "C:\Doc1.doc," and then quit Word.
  2. Start a new project in Microsoft Visual Basic.
  3. On the Project menu, click References. Select the object Library for your version of Microsoft Word, and then click OK.
  4. 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
  5. 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.

REFERENCES

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:
184974 WD: How to Use (OLE) Automation with Word
220607 How To Automate Microsoft Word to Perform Mail Merge from Visual Basic
220911 How To Automate Microsoft Word to Perform a Mail Merge Using Visual C++ and MFC
244219 How To Automate MailMerge in Word 2000 Using Visual J++ ( Java )

Properties

Article ID: 258523 - Last Review: April 3, 2007 - Revision: 4.1
APPLIES TO
  • Microsoft Office Word 2007
  • Microsoft Office Word 2003
  • Microsoft Word 2002
  • Microsoft Word 2000
  • 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 Enterprise Edition for Windows 6.0
Keywords: 
kbautomation kbhowto KB258523

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com