Free Support Forum - aspose.com

MailMerge with Regions some problem?

Hello,

I am trying to work with the Mail Merge with regions. but it seems I am doing something wrong.

Below method is simply building the document as per the template provided, see the attached student.doc file

public void BuildReport(DataTable table)

{

Document doc = new Document("student.doc");

table.TableName = "Students";

doc.MailMerge.ExecuteWithRegions(table);

doc.Save("Out.doc");

}

My datatable is having 99 rows which I am passing to this method. But dont know wherre I am mistaking.

Select StudentName from Student where StudentID < 1000

This query above is brining 99 records but using the above mentioned method it is not writing into the word document.

Thanks and best regards

Austinn.

Hi

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

Thanks for your inquiry. I found some mistakes in your template. You should note, for changing field name please right click on the merge field and select Edit Field from context menu and change field name. I attached the changed template here. I use the following code to fill the document using MailMerge with regions:

// Open document

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

DataTable dt = GetStudents();

doc.MailMerge.ExecuteWithRegions(dt);

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

public DataTable GetStudents()

{

// Create dummy datasource.

DataTable data = new DataTable("Students");

data.Columns.Add("StudentName");

// Add few rows.

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

data.Rows.Add(new object[] { "Name " + i });

return data;

}

Hope this helps.

Best regards,

Thanks alot Andreyn, actually I am creating the merge fields dynamically using the c# code. can you please tell me what is the problem. coz I need to facilitate the user that what ever field he/she needs from the students and cources table they can selecte and prepare the template...instead of using MS WORD to build the document manually.

So please provide me some details about the problem. I am pasting the code which is being used to generate the merge field template

Document document = new Document();

DocumentBuilder docBuilder = new DocumentBuilder(document);

MailMerge mailMerge = document.MailMerge;

docBuilder.InsertField(@"MERGEFIELD ®TableStart:Students¯ \* MERGEFORMAT", "®TableStart:Students¯\t");

// this is the arraylist which is passed to this piece of code to generate the selected fields as mergefields

for (int i = 0; i < _fileds.Count; i++)

{

docBuilder.InsertField(@"MERGEFIELD " + _fileds[i].ToString() + @"\* MERGEFORMAT", "®" + _fileds[i].ToString() + "¯\t");

}

docBuilder.InsertField(@"MERGEFIELD ®TableEnd:Students¯ \* MERGEFORMAT", "®TableEnd:Students¯");

document.Save("Student.doc");

Will look forward to hear from you soon.

Thanks and regards

Austinn

Hi

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

Thank you for additional information. The problem occurs because there are some special character and the beginning of the field name and at its end.

docBuilder.InsertField(@"MERGEFIELD ®TableStart:Students¯ \* MERGEFORMAT", "®TableStart:Students¯\t");

There should not be these characters. Your code should look like the following:

docBuilder.InsertField(@"MERGEFIELD TableStart:Students", "«TableStart:Students»");

Best regards,

Thanks alot AndreyN. really it worked and am able to fix my code.