Microsoft KB Archive/840975

= How to merge each record of data source as separate documents in Word =

Article ID: 840975

Article Last Modified on 1/30/2007

-

APPLIES TO


 * Microsoft Office Word 2003
 * Microsoft Word 2002 Standard Edition
 * Microsoft Word 2000 Standard Edition
 * Microsoft Word 97 Standard Edition
 * Microsoft Office Word 2007

-



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 Office 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 &quot;as is&quot; 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 &quot;as is&quot; 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 = &quot;Enter the number of the first record to be printed &quot; _ & &quot;(from 1 to &quot; & x & &quot;)&quot; v = CLng(InputBox(stMsg, &quot;First Record&quot;)) If v < 1 Then GoTo Err_Handler stMsg = &quot;Enter the number of the last record to be printed &quot; _ & &quot;from &quot; & v + 1 & &quot; to &quot; & x  If v < x Then w = CLng(InputBox(stMsg, &quot;Last Record&quot;)) 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:=&quot;That is not a valid record number.&quot;, Title:=&quot;Invalid record number&quot; End Sub

Additional query words: WD2003 WD2007 WD2002

Keywords: kbhowto kbinfo KB840975

-

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

© Microsoft Corporation. All rights reserved.