How to merge each record of data source as separate documents in Word (840975)



The information in this article applies to:

  • Microsoft Office Word 2003
  • Microsoft Word 2002
  • Microsoft Word 2000
  • Microsoft Word 97 for Windows

INTRODUCTION

This article lists two Microsoft Visual Basic for Applications (VBA) macros that you can use to merge a data source from your main document to your printer in Microsoft Word.

The MergeAllRecordsToPrinter macro will merge all records in your data source to your printer. Each record in your data source will print as a separate document.

The MergeSelectedRecordsToPrinter macro lets you select the records of your data source that you want to merge to your printer. Each record in your data source that you choose to merge will print as a separate document.

MORE INFORMATION

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.

MergeAllRecordsToPrinter macro

Sub MergeAllRecordsToPrinter()
'
' MergeAllRecordsToPrinter Macro
'WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS EXAMPLE IS
'AT YOUR OWN RISK. Microsoft provides this macro code "as is" without warranty of
'any kind, either express or implied, including but not limited to the implied warranties of
'merchantability and/or fitness for a particular purpose.
'
Dim x As Long
Dim i As Long

 With ActiveDocument.MailMerge
   
   .Destination = wdSendToNewDocument
   .SuppressBlankLines = True
   
   'get the record count of the datasource
   With .DataSource
     .ActiveRecord = wdLastRecord
     x = .ActiveRecord
     'set the activerecord back to the first
     .ActiveRecord = wdFirstRecord
    
   End With
   
   'loop the datasource count and merge one record at a time
   For i = 1 To x
     .DataSource.FirstRecord = i
     .DataSource.LastRecord = i
     .Execute Pause:=True
      ActiveDocument.PrintOut
      ActiveDocument.Close wdDoNotSaveChanges
   
   Next i
 End With
End Sub

MergeSelectedRecordsToPrinter macro

Sub MergeSelectedRecordsToPrinter()
'
' MergeSelectedRecordsToPrinter Macro
'WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS EXAMPLE IS
'AT YOUR OWN RISK. Microsoft provides this macro code "as is" without warranty of
'any kind, either express or implied, including but not limited to the implied
'warranties of merchantability and/or fitness for a particular purpose.
'
Dim x As Long
Dim i As Long
Dim v As Long, w As Long
Dim stMsg As String

On Error GoTo Err_Handler

 With ActiveDocument.MailMerge
   
   .Destination = wdSendToNewDocument
   .SuppressBlankLines = True
   
   'get the record count of the datasource
   With .DataSource
     .ActiveRecord = wdLastRecord
     x = .ActiveRecord
     'set the activerecord back to the first
     .ActiveRecord = wdFirstRecord
    
   End With
   
   ' Ask for the record range
   stMsg = "Enter the number of the first record to be printed " _
         & "(from 1 to " & x & ")"
   v = CLng(InputBox(stMsg, "First Record"))
   If v < 1 Then GoTo Err_Handler
   
   
   stMsg = "Enter the number of the last record to be printed " _
         & "from " & v + 1 & " to " & x
   If v < x Then
      w = CLng(InputBox(stMsg, "Last Record"))
      If w > x Then GoTo Err_Handler
   Else
      w = x
      
   End If
   
   'loop the datasource count and merge one record at a time
   For i = v To w
     .DataSource.FirstRecord = i
     .DataSource.LastRecord = i
     .Execute Pause:=True
      ActiveDocument.PrintOut
      ActiveDocument.Close wdDoNotSaveChanges

   Next i
 End With
 Exit Sub
 
Err_Handler:
 
 MsgBox Prompt:="That is not a valid record number.", Title:="Invalid record number"
 
End Sub

REFERENCES

For more information about how to create a macro in Word, click Microsoft Word Help on the Help menu, type create a macro in the Search for box in the Assistance pane, and then click Start searching to view the topic.
For more information about how to run a macro in Word, click Microsoft Word Help on the Help menu, type run a macro in the Search for box in the Assistance pane, and then click Start searching to view the topic.

Modification Type:MajorLast Reviewed:3/23/2006
Keywords:kbhowto kbinfo KB840975 kbAudEndUser