You receive a "Run-time error '5631'" error message when you use a VBA macro to create a mail merge in Word 2003 (828388)



The information in this article applies to:

  • Microsoft Office Word 2003

SYMPTOMS

When you use a Microsoft Visual Basic for Applications (VBA) macro to programmatically create a mail merge 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 "This is the added line to save the data document."
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:="data.doc", HeaderRecord:="First" & Application.International(wdListSeparator) & "Last"
        oMM.EditDataSource
        Set oDataDoc = Application.ActiveDocument
        Set oTable = oDataDoc.Tables(1)
        oTable.Rows.Add
        oTable.Rows.Add
        oTable.Cell(2, 1).Range = "Sidney"
        oTable.Cell(2, 2).Range = "Higa"
        oTable.Cell(3, 1).Range = "Kim"
        oTable.Cell(3, 2).Range = "Ralls"
        oTable.Cell(4, 1).Range = "Bjorn"
        oTable.Cell(4, 2).Range = "Rettig"
        '*******
        oDataDoc.Save 'This is the added line to save the data document.
        '*******
        oMM.Fields.Add oDoc.Content, "First"
        oMM.DataSource.FirstRecord = 1
        oMM.Execute    
    End Sub

Modification Type:MajorLast Reviewed:3/23/2006
Keywords:kberrmsg kbprb KB828388 kbAudDeveloper kbAudEndUser