How to determine the number of records that will be merged in a Word mail merge document before you run Mail Merge

For a Microsoft Word 2002 version of this article, see 258523 .

Summary

This article describes how to determine the number of records that will be merged in a Microsoft Office Word 2003 mail merge document or in a Microsoft Office Word 2007 mail merge document before you run Mail Merge.

More Information

You can determine the number of records that will be merged in a Word document before you run Mail Merge by setting the ActiveRecord property of the DataSource object to wdLastRecord. Then you can query the ActiveRecord property for the number of records. To do this, follow these steps:
  1. Use the appropriate method:
    • Start Word 2003, and then follow these steps:
      1. On the File menu, click
        New. On the Tools menu, click Letters and Mailings, and then click Mail Merge.
      2. Click
        Next to accept Letters as the document type.
      3. Click Next to accept Use the current document.
      4. Click Browse. In the
        Select Data Source dialog box, move to the Northwind.mdb sample database, and then click Open. In the Select Table dialog box, click the Customers table, and then click OK. In the Mail Merge Recipients dialog box, click OK.

        Note The default location for Access sample databases is C:\Program Files\Microsoft Office\Office11\Samples.
      5. Click Next.
      6. Click More Items.

        The Insert Merge Field dialog box appears.
      7. Click CustomerID, click Insert, and then click
        Close.
      8. Save the document as
        C:\Doc1.doc. Quit Word.
    • Start Word 2007, and then follow these steps:
      1. On the Mailings tab, click Start Mail Merge, and then click Step by Step Mail Merge Wizard.
      2. Click
        Next to accept Letters as the document type.
      3. Click Next to accept Use the current document.
      4. Click Browse. In the
        Select Data Source dialog box, click Northwind 2007.accdb, and then click Open. In the Select Table dialog box, click the Customers table, and then click OK. In the Mail Merge Recipients dialog box, click OK.
      5. Click Next.
      6. Click More Items.
      7. In the Insert Merge Field dialog box, click ID, click Insert, and then click
        Close.
      8. Save the document as
        C:\Doc1.doc, and then exit Word.
  2. In Microsoft Visual Basic, start a new Standard EXE project.
  3. On the Project menu, click
    References. Click Microsoft Word 11.0 Object Library or Microsoft Word 12.0 Object Library, and then click OK.
  4. Add a CommandButton control to Form1, and then add the following code to the Click event of the CommandButton control:
    Private Sub Command1_Click()

    Dim oApp As Word.Application
    Dim oDoc As Word.Document

    'Create 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 will be merged.
    Ret = MsgBox(.DataSource.ActiveRecord & " records will be " & _
    "merged. Click Yes to continue or No to quit.", _
    vbYesNo + VbMsgBoxSetForeground)

    If Ret = vbYes Then
    'Continue with the mail merge.
    .Execute
    Else
    'Quit Word. Do not save any changes.
    oApp.Quit False
    End If

    End With

    End Sub
  5. Press F5 to run the program, and then click
    Command1.

    Note The mail merge document opens. The number of records that will be merged appears.

    Click Yes to continue Mail Merge. Click No to cancel Mail Merge and to quit Word.


Note You may receive the following error message during execution:

Run-time error '5852':
Requested object is not available.
This error occurs because Word 2003 or Word 2007 displays a message box when you try to open a Word 2003 mail merge document that is linked to a data source. The default value is No, and Word 2003 or Word 2007 will not run the SQL command.

However, if you open the mail merge document in code by using automation, you have no opportunity to select Yes. The default value No is used. Therefore, the SQL command does not run and you cannot gain access to the data. To make the previous code sample run correctly, you must create the following registry key for Word 2003:
HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\Word\Options

SQLSecurityCheck=dword:00000000
Or, you must create the following registry key for Word 2007:
HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Word\Options

SQLSecurityCheck=dword:00000000
For additional information about how to do this, click the following article number to view the article in the Microsoft Knowledge Base:
825765 "Opening This Will Run the Following SQL Command" Message When You Open a Word Document

References

For additional information about automating Microsoft Word to perform Mail Merge, click the following article numbers to view the articles in the Microsoft Knowledge Base:

184974 OFF: How to Use (OLE) Automation with Word

220607 HOWTO: Automate Microsoft Word to Perform Mail Merge from Visual Basic
220911 HOWTO: Automate Microsoft Word to Perform a Mail Merge Using Visual C++ and MFC
244219 HOWTO: Automate MailMerge in Word 2000 Using Visual J++ ( Java )
Propriedades

ID do Artigo: 829121 - Última Revisão: 23 de mar de 2009 - Revisão: 1

Comentários