How To Determine the Number of Merged Records Before Executing a Mail Merge (258523)



The information in this article applies to:

  • Microsoft Word 2002
  • Microsoft Word 2000
  • Microsoft Word 97 for Windows
  • Microsoft Visual Basic Professional Edition for Windows 5.0
  • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic Enterprise Edition for Windows 5.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0

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:
  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 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.
    Microsoft Word 2002

    1. Start a new document. On the Tools menu, click Letters and Mailings, and then select Mail Merge Wizard.
    2. In Step1 of the Mail Merge Wizard, click Next to accept Form Letters as the document type.
    3. In Step2, click Next to use the current document for the form letters.
    4. In Step3, 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 dialogbox 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 databases is:

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

    5. In Step3 of the Mail Merge Wizard, click Next.
    6. In Step4 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.
  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 the F5 key to run the program and click Command1.

    Results: The Mail Merge Document is opened and the number of records that are to be merged is displayed. Click Yes to proceed with the mail merge or click No to cancel the mail merge and quit 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 )


Modification Type:MinorLast Reviewed:6/30/2004
Keywords:kbAutomation kbhowto KB258523 kbAudDeveloper