How To: Determine the Number of Merged Records Before You Run a Mail Merge (829121)



The information in this article applies to:

  • Microsoft Office Word 2003
  • 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

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 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. Start Word 2003.
    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.
  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, 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 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 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:

HKEY_CURRENT_USER\Software\Microsoft\Office\11.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 )


Modification Type:MajorLast Reviewed:3/23/2006
Keywords:kbAutomation kbhowto KB829121 kbAudDeveloper