Hi
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
Thanks for your inquiry. Please follow the link to learn how to produce multiple documents upon mail merge:
Also, I think the following article could be useful for you:
Please let me know if you need more assistance I will be glad to help you.
Best regards,
Thanks for the reply, but I think you missunderstand me.
Hi there,
Thanks for this additional information.
I'm afraid it's still not quite clear what you are trying to achieve, could you please post your datasource here and prehaps some example output? It does seem that the link Alexey provided above does describe how to achieve what your asking. Are you by chance using nested mail merge and want each child row to appear in a new document?
Thanks,
aske012:
Are you by chance using nested mail merge and want each child row to appear in a new document?
Hi there,
Thank you for this additonal information. What you're asking sounds very complex, I suppose the implementation would be something such as below, although it doesn't seem to make sense if you have more than one separate region on your template.
// Used to name the output documents.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
int recordIndex = 0;
// Your dataset with all data and relations populated
DataSet dataSet = GetData();
// Define where to save the merged document output to
string path = dataDir + @"\Output Documents\" + "Merged Document {0}.doc";
// Create a temporary dataset containing only the rows we want to merge for one document
DataSet tempDataSet = dataSet.Clone();
foreach (DataTable table in dataset.Tables)
{
// Encountered a regular datatable with no child tables and is not the child of another table
if (table.ChildRelations.Count == 0 && table.ParentRelations.Count == 0)
{
foreach(DataRow row in table.Rows)
{
// Import this row into the temporary datatable in the dataset
DataTable tempTable = tempDataSet.Tables[table.TableName];
tempTable.ImportRow(row);
GenerateDocument(doc, tempDataSet, string.Format(path, recordIndex));
tempTable.Rows.RemoveAt(0);
recordIndex++;
}
}
else
{
// Encountered a datatable with child tables, iterate through all of the relations, merge those rows and save them to separate documents.
foreach (DataRelation relation in table.ChildRelations)
{
foreach (DataRow parentRow in table.Rows)
{
// Import this row into the temporary datatable in the dataset
DataTable parentTable = tempDataSet.Tables[parentRow.Table.TableName];
tempDataSet.Tables[parentRow.Table.TableName].ImportRow(parentRow);
foreach (DataRow childRow in parentRow.GetChildRows(relation))
{
// Import this row into the temporary datatable in the dataset
DataTable childTable = tempDataSet.Tables[childRow.Table.TableName];
childTable.ImportRow(childRow);
// Merge and save document
GenerateDocument(doc, tempDataSet, string.Format(path, recordIndex));
recordIndex++;
// Remove this row to make the table empty for the next merge
childTable.Rows.RemoveAt(0);
}
// Remove this row to make the table empty for the next merge
parentTable.Rows.RemoveAt(0);
}
}
}
}
public static void GenerateDocument(Document origDoc, DataSet dataSet, string path)
{
Document subDoc = origDoc.Clone();
subDoc.MailMerge.ExecuteWithRegions(dataSet);
subDoc.Save(path);
}
If you have any further issues can you please attach your template, data source (you can save your DataSet into XML format and attach it here by using the DataSet.WriteXml method in your application) and also a quick example document of how you would want one of the outputs to look like.
Thanks,
Hi there,
Thanks for this additional information. I'm afraid it's still not clear as to what exactly you are looking to achieve. Could you clarify exactly how you would merge a document with multiple tables and nested relations and expect a each data record to be merged into a separate document? What would happen to the other regions that are not merged in your template?
As I suggested, if you post your template and datasource here for testing then I can give some further suggestions. I may also be able to log a request for such a feature.
Thanks,
I’ve attached the datasource as xml, the template and the merged document
Hi
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
Thank you for additional information.
1. Your template contains nested regions. To fill nested regions you need to setup relationships in your dataset, and to do that you need to know columns names.
2. I think the only way to achieve what you need is creating a custom parser of your XML. As a result of parsing you need to get a data table, each row of this data table should contain data, which should be merged into each separate document. In this case you will also need to remove regions from your template and use simple mail merge as described here:
Best regards,
Hi,