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.
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;
}
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