Free Support Forum - aspose.com

How to repeat some table fields but not others

I'm attempting to do a mail merge from a table that looks something like:

Client ID QuestionID

1 13

1 45

I'd like to build a word doc that has the client fields once, but then repeats the Question fields. If i create the document as "TableStart ... Client Fields.... Question Fields... TableEnd", then all fields are repeated. If i create the document as "Client Fields.... TableStart ... Question Fields... TableEnd", then the client fields do not merge in.

How can a create a doc where some table fields are merged once, but others are repeated?

Thanks, Jon

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

Thanks for your request. I think that you can achieve this using MergeField event handler. For example see the attached document and the following code:

public void Test156()

{

//Create DataSource

DataTable myTable = new DataTable("myTable");

myTable.Columns.Add("ClientID", typeof(int));

myTable.Columns.Add("QuestionID", typeof(int));

for (int i = 0; i < 10; i++)

{

myTable.Rows.Add(new object[] { 1, i });

}

//Open document

Document doc = new Document(@"Test156\in.doc");

//Add MergeField event handler

doc.MailMerge.MergeField += new MergeFieldEventHandler(MailMerge_MergeField156);

//Execute mailmerge

doc.MailMerge.ExecuteWithRegions(myTable);

//Save output

doc.Save(@"Test156\out.doc");

}

//Flag that indicates if Client Id is already merged

bool clientIDMerged = false;

void MailMerge_MergeField156(object sender, MergeFieldEventArgs e)

{

if (e.FieldName == "ClientID")

{

if (!clientIDMerged)

clientIDMerged = true; //Set flag

else

e.Text = string.Empty; //Ignore field

}

}

I hope this could help you.

Best regards.

Hi,

Your source code is very useful for me. But, I am not handle any merge field events in my application.

I am using trial version. Please let me know Should I import any other classes or not?

I have imported this only...

Imports Aspose.Words

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

Thanks for your request. You should also import Aspose.Words.Reporting namespace:

Imports Aspose.Words.Reporting

Best regards.