We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to produce only one document during mail merge

I applying mail merge and want to produce only one document.

Currently each time the mail merge is executed, it saves that merged document, but I want to add that document as a page in one document rather than saving it in a separate document.
Below is my code:

// Loop though all records in the data source.
foreach (DataRow row in data.Rows)
{
// Clone the template instead of loading it from disk (for speed).
Document dstDoc = (Document)doc.Clone(true);

// Execute mail merge.
dstDoc.MailMerge.Execute(row);

//Insert Signature Image
DocumentBuilder builder = new DocumentBuilder(dstDoc);

builder.InsertImage(@“d:\Test.jpg”,
RelativeHorizontalPosition.Margin,
200,
RelativeVerticalPosition.Margin,
110,
120,
60,
WrapType.Square);

// Save the document.
builder.Document.Save(string.Format(@“c:_Employees\EmployeeVoucher_ {0}.doc”, counter++));

}
Hi Johnane,

Thanks for your inquiry. As you are performing mail merge and then inserting image at the end of document for each DataRow. In your case, we suggest you please insert an image mail merge field into template document with field name Image:MyFieldName. In MS Word, select Insert/Field command, then select MergeField and type Image:MyFieldName.

Please implement IFieldMergingCallback interface if you want to control how data is inserted into merge fields during a mail merge operation. IFieldMergingCallback.ImageFieldMerging method is called when the Aspose.Words mail merge engine is about to insert an image into a merge field. Please pass DataTable to MailMerge.Execute method.

If you want to use DataRow as you are using in your code, please use following code example to achieve your requirements. Hope this helps you.

Document maindoc = new Document();

DocumentBuilder builder = new DocumentBuilder(maindoc);

Document doc = new Document(MyDir + "in.docx");

DataTable data = null;

foreach (DataRow row in data.Rows)

{

// Clone the template instead of loading it from disk (for speed).

Document dstDoc = (Document)doc.Clone(true);

// Execute mail merge.

dstDoc.MailMerge.Execute(row);

//Insert Signature Image

DocumentBuilder dstbuilder = new DocumentBuilder(dstDoc);

builder.InsertImage(@"d:\Test.jpg",

RelativeHorizontalPosition.Margin,

200,

RelativeVerticalPosition.Margin,

110,

120,

60,

WrapType.Square);

builder.InsertDocument(dstDoc, ImportFormatMode.UseDestinationStyles);

}

maindoc.Save(MyDir + "Out.docx");

There is no builder.InsertDocument property.

How come?
Hi Johnane,

Thanks for your inquiry. The DocumentBuilder.InsertDocument method was introduced in Aspose.Words v15.3.0. Please check the public API changes from following link.
Public API Changes in Aspose.Words 15.3.0

We suggest you please upgrade to the latest version of Aspose.Words for .NET 16.1.0 and let us know if you have any more queries.

Thanks

That seemed to solve the problem
Great work
Hi Johnane,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.