Hello:
I've tried your new document file and code but the output is not generating properly. This is the modified code:
private void MyMethod()
{
DataTable customer = new DataTable("Customer");
customer.Columns.Add("CustName");
customer.Columns.Add("Phone");
DataRow row = customer.NewRow();
row[0] = "test1111";
row[1] = "991233445,992783946,99545654494,9945646546565,9945646546565";
customer.Rows.Add(row);
DataRow row1 = customer.NewRow();
row1[0] = "test22222";
row1[1] = "5233445,7783946";
customer.Rows.Add(row1);
DataRow row2 = customer.NewRow();
row2[0] = "test3333";
row2[1] = "551233445,552783946,55545654494,55545646546565";
customer.Rows.Add(row2);
//open template
//Document doc = new Document("C:\\Inetpub\\wwwroot\\AsposeTest\\Documents\\in1.doc");
Document doc = new Document("C:\\Inetpub\\wwwroot\\AsposeTest\\Documents\\inMy.doc");
doc.Sections[1].Remove();
doc.MailMerge.MergeField += new MergeFieldEventHandler(MailMerge_MergeField_103385);
doc.MailMerge.Execute(customer);
//save document
doc.Save("C:\\Inetpub\\wwwroot\\AsposeTest\\Documents\\outMy.doc");
}
//--------------------------------------------------------------------
void MailMerge_MergeField_103385(object sender, MergeFieldEventArgs e)
{
if (e.FieldName == "Phone")
{
DocumentBuilder builder = new DocumentBuilder(e.Document);
builder.MoveToMergeField("PhoneList");
string[] phoneArr = e.FieldValue.ToString().Split(',');
if (phoneArr.Length > 2)
{
e.Text = string.Format("{0},{1}", phoneArr[0], phoneArr[1]);
//create short phonelist
DataTable phones = new DataTable("Customer");
phones.Columns.Add("Phone");
for (int i = 0; i < phoneArr.Length; i++)
{
DataRow phoneRow = phones.NewRow();
phoneRow[0] = phoneArr[i];
phones.Rows.Add(phoneRow);
}
//Document doc2 = new Document("C:\\Inetpub\\wwwroot\\AsposeTest\\Documents\\in2.doc");
Document doc2 = new Document("C:\\Inetpub\\wwwroot\\AsposeTest\\Documents\\inMy.doc");
doc2.Sections[0].Remove();
doc2.MailMerge.ExecuteWithRegions(phones);
builder.InsertBreak(BreakType.PageBreak);
InsertDocument(builder.CurrentParagraph, doc2);
}
}
}
//--------------------------------------------------------------------
public void InsertDocument(Node insertAfterNode, Document srcDoc)
{
// We need to make sure that the specified node is either pargraph or table.
if (!((insertAfterNode.NodeType == NodeType.Paragraph) ||
(insertAfterNode.NodeType == NodeType.Table)))
throw new ArgumentException("The destination node should be either paragraph or table.");
// We will be inserting into the parent of the destination paragraph.
CompositeNode dstStory = insertAfterNode.ParentNode;
// This object will be translating styles and lists during the import.
NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.Document,
ImportFormatMode.KeepSourceFormatting);
// Loop through all sections in the source document.
foreach (Section srcSection in srcDoc.Sections)
{
// Loop through all block level nodes (paragraphs and tables) in the body of the section.
foreach (Node srcNode in srcSection.Body)
{
// Do not insert node if it is a last empty paragarph in the section.
Paragraph para = srcNode as Paragraph;
if ((para != null) && para.IsEndOfSection && !para.HasChildNodes)
break;
// This creates a clone of the node, suitable for insertion into the destination document.
Node newNode = importer.ImportNode(srcNode, true);
// Insert new node after the reference node.
dstStory.InsertAfter(newNode, insertAfterNode);
insertAfterNode = newNode;
}
}
}
The input and output files are attached. please see the attacment. My desired output is exactly you done using two separate files. I'm in big trouble.
Need your help.
Thanx-
Razin