Microsoft KB Archive/828388

= You receive a “Run-time error ‘5631’” error message when you use a VBA macro to create a mail merge in Word 2007 or in Word 2003 =

Article ID: 828388

Article Last Modified on 1/25/2007

-

APPLIES TO


 * Microsoft Office Word 2003
 * Microsoft Office Word 2007

-





SYMPTOMS
When you use a Microsoft Visual Basic for Applications (VBA) macro to programmatically create a mail merge in Microsoft Office Word 2007 or in Microsoft Office Word 2003, you may receive an error message that is similar to the following:

Run-time error '5631'

Word could not merge the main document with the data source because the data records were empty or no data records matched your query options.



CAUSE
This issue occurs when the data document has not been saved before the MailMerge.Execute method runs.



RESOLUTION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. To resolve this issue, add a line to the macro to save the data document; add this line immediately after the last change to the data document and just before the line that starts the MailMerge.Execute method. In the following sample code, the additional line is followed by a comment that states &quot;This is the added line to save the data document.&quot; Sub PrintMerge Dim oDoc As Document Dim oMM As MailMerge Dim oDataDoc As Document Dim oTable As Table Set oDoc = Application.Documents.Add Set oMM = oDoc.MailMerge oMM.MainDocumentType = wdFormLetters oMM.CreateDataSource Name:=&quot;data.doc&quot;, HeaderRecord:=&quot;First&quot; & Application.International(wdListSeparator) & &quot;Last&quot; oMM.EditDataSource Set oDataDoc = Application.ActiveDocument Set oTable = oDataDoc.Tables(1) oTable.Rows.Add oTable.Rows.Add oTable.Cell(2, 1).Range = &quot;Sidney&quot; oTable.Cell(2, 2).Range = &quot;Higa&quot; oTable.Cell(3, 1).Range = &quot;Kim&quot; oTable.Cell(3, 2).Range = &quot;Ralls&quot; oTable.Cell(4, 1).Range = &quot;Bjorn&quot; oTable.Cell(4, 2).Range = &quot;Rettig&quot; '*******       oDataDoc.Save 'This is the added line to save the data document. '*******       oMM.Fields.Add oDoc.Content, &quot;First&quot; oMM.DataSource.FirstRecord = 1 oMM.Execute End Sub

Additional query words: kbnoOfficeAlertID WD2007

Keywords: kberrmsg kbprb KB828388

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.