Free Support Forum -

VB.NET only mail merging one page

I’m testing to see if the product can handle our needs which I believe does but when I run the code below only one page is generated even though the system goes through a for next loop from 1 to 3. Any guidance is appreciated.

Dim sFilename As String = “c:\LargeDonorGen.doc”

Dim doc As Aspose.Word.Document = New Aspose.Word.Document(sFilename)

Dim iX As Long

For iX = 1 To 3

doc.MailMerge.Execute( _

New String() {“FullName”, “FirstName”, “LastName”, “Address1”, “City”, “State”, “Zip”, “ReceiptNumber”, “GiftDate”, “GiftAmount”}, _

New Object() {iX & " - John C. Marx", “John”, “Marx”, “802 Madison Street”, “Michigan City”, “IN”, “46360”, “PLDG000000000001”, “01/01/2005”, “$999,999.99”})


doc.Save(“c:\abc.doc”, Aspose.Word.SaveFormat.FormatDocument)

A succesful mail merge replaces all fields in the document with data (means deletes mail merge fields). That’s why you consequtive mail merges do nothing.

Depending on what you want to achieve there is a number of ways to do it.

If you had your data in a DataTable for example, then calling one mail merge will work because it will duplicate the content for each record automatically before merging.

If you want to produce many documents each for one record, then you either need to open the original document before every mail merge or open it once and clone before every mail merge.

My objective is to loop through and validate on a SQL data reader and then pass each item. I tried a simple for/next loop as that should produce the same as what I want to achieve. Do you have a simple sample of that?

If you have a DataTable, DataView or DataReader you can use this:

See topics about mail merge in the Programmers Guide

Also see source code of the demo project included with the installer.