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

Free Support Forum - aspose.com

How to insert the new line for each row of a Datatable in mail merge?

Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

«TableStart:Emp»

«FirstName» «LastName»

«TableEnd:Emp»

The above is my merge fields in my word document, for example if my data table contains the two employees then it will display the records like the below way

John Smith

Alexander Smith

Now my problem is I want to give one line break for each row, please advise how to do this?

I want like this

John Smith

Alexander Smith

Thanks,

Sburepalli.v

Hi Sburepalli,

You can add a line break in your template after last name field. If you want to use Aspose.Words for that purpose, you can implement IFieldMergingCallback as you can see in the following code.

class HandleMergeField : IFieldMergingCallback

{

private DocumentBuilder mBuilder;

void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)

{

if (mBuilder == null)

mBuilder = new DocumentBuilder(args.Document);

<?xml:namespace prefix = "o" ns = "urn:schemas-microsoft-com:office:office" />

if (args.FieldName == "LastName")

{

string updatedValue = args.FieldValue+"\n";

mBuilder.MoveToMergeField(args.FieldName);

mBuilder.Write(updatedValue);

valueChange = true;

}

}

void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)

{

// Do nothing.

}

}

Please check http://www.aspose.com/docs/display/wordsnet/How+to++Apply+Custom+Formatting+during+Mail+Merge for more details.

Best Regards,

Hi,

Thank you so much for your quick reply.

Formatting within the word document worked, Thank you so much for this.

coming to, by using the aspose the attached code did not worked. In the same code you mentioned valueChange what is this? this variable is not declared anywhere.

Could you please let me know I am eager to know.

Thanks,

BVS Rao.

Hi BVS Rao,

Following is my complete code and sample document and XML are also attached.

using Aspose.Words;

using Aspose.Words.Reporting;

using System.Data;

<?xml:namespace prefix = "o" ns = "urn:schemas-microsoft-com:office:office" />

namespace MailMergeIssue

{

class Program

{

static void Main(string[] args)

{

// Open an existing document.

Document doc = new Document("Sample.docx");

// Add a handler for the MergeField event.

doc.MailMerge.FieldMergingCallback = new HandleMergeField();

DataSet dataSet = new DataSet();

dataSet.ReadXml("Sample.xml");

// Fill the fields in the document with user data.

doc.MailMerge.ExecuteWithRegions(dataSet.Tables[0]);

doc.Save("Sample_Out.docx");

}

}

class HandleMergeField : IFieldMergingCallback

{

private DocumentBuilder mBuilder;

void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)

{

if (mBuilder == null)

mBuilder = new DocumentBuilder(args.Document);

if (args.FieldName == "LastName")

{

string updatedValue = args.FieldValue + "\n";

mBuilder.MoveToMergeField(args.FieldName);

mBuilder.Write(updatedValue);

}

}

void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)

{

// Do nothing.

}

}

}

Best Regards,