Hi
Thank you for additional information. I suppose you use code like the following:
// Open source document.
Document doc1 = new Document(@“Test001\Fahrraddiebstahl.doc”);
Document doc2 = new Document(@“Test001\Elementar1.doc”);
Document doc3 = new Document(@“Test001\Elementar2.doc”);
Document doc4 = new Document(@“Test001\Elementar3.doc”);
Document doc5 = new Document(@“Test001\Haushaltsglas.doc”);
// Prepare datasource for mail merge.
string[] names = { “tb_Elementarschaden1”, “tb_Fahrraddiebstahl”, “tb_Elementarschaden2”, “tb_Elementarschaden3”, “tb_Haushaltsglas” };
// Plrepare array with data.
object[] values = { ConvertDocumentToHtml(doc1),
ConvertDocumentToHtml(doc2),
ConvertDocumentToHtml(doc3),
ConvertDocumentToHtml(doc4),
ConvertDocumentToHtml(doc5) };
// Open template document.
Document doc = new Document(@“Test001\main.doc”);
// Add MergeField event handler
doc.MailMerge.MergeField += new MergeFieldEventHandler(MailMerge_InsertHtml);
doc.MailMerge.RemoveEmptyParagraphs = true;
// Execute mail merge
doc.MailMerge.Execute(names, values);
// Save output docuemnt
doc.Save(@“Test001\out.doc”);
===================================================================
void MailMerge_InsertHtml(object sender, MergeFieldEventArgs e)
{
// Create document builder
DocumentBuilder builder = new DocumentBuilder(e.Document);
// Move builder cursor to the mergefield
builder.MoveToMergeField(e.FieldName);
// Insert HTML
builder.InsertHtml(e.FieldValue.ToString());
e.Text = string.Empty;
}
===================================================================
public string ConvertDocumentToHtml(Document doc)
{
string html = string.Empty;
// Save docuemnt to MemoryStream in Hml format
using (MemoryStream htmlStream = new MemoryStream())
{
doc.Save(htmlStream, SaveFormat.Html);
// Get Html string
html = Encoding.UTF8.GetString(htmlStream.GetBuffer(), 0, (int)htmlStream.Length);
}
// There could be BOM at the beggining of the string.
// We should remove it from the string.
while (html[0]!=’<’)
html = html.Substring(1);
return html;
}
This code works correctly on my side. I tried different order of fields. Could you please check this code on your side and let me know if it works fine?
Best regards.