Microsoft KB Archive/829121

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

Article ID: 829121

Article Last Modified on 3/30/2007

-

APPLIES TO


 * Microsoft Office Word 2007
 * Microsoft Office Word 2003
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-





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:  Use the appropriate method:  Start Word 2003, and then follow these steps:  On the File menu, click New. On the Tools menu, click Letters and Mailings, and then click Mail Merge. Click Next to accept Letters as the document type. Click Next to accept Use the current document. 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.</li> Click Next.</li> Click More Items.

The Insert Merge Field dialog box appears.</li> Click CustomerID, click Insert, and then click Close.</li> Save the document as C:\Doc1.doc. Quit Word.</li></ol> </li> Start Word 2007, and then follow these steps: <ol style="list-style-type: lower-alpha;"> On the Mailings tab, click Start Mail Merge, and then click Step by Step Mail Merge Wizard.</li> Click Next to accept Letters as the document type.</li> Click Next to accept Use the current document.</li> 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.</li> Click Next.</li> Click More Items.</li> In the Insert Merge Field dialog box, click ID, click Insert, and then click Close.</li> Save the document as C:\Doc1.doc, and then exit Word.</li></ol> </li></ul> </li> In Microsoft Visual Basic, start a new Standard EXE project.</li> On the Project menu, click References. Click Microsoft Word 11.0 Object Library or Microsoft Word 12.0 Object Library, and then click OK.</li>  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(&quot;C:\doc1.doc&quot;) '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 & &quot; records will be &quot; & _                   &quot;merged. Click Yes to continue or No to quit.&quot;, _                    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 </li> <li>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.</li></ol>

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:

Or, you must create the following registry key for Word 2007:

For additional information about how to do this, click the following article number to view the article in the Microsoft Knowledge Base:

825765 &quot;Opening This Will Run the Following SQL Command&quot; Message When You Open a Word Document

<div class="references_section">