How can I save a bulk letter and each document in the database?

I have one Template (with Regions) and I would like to merge and save a bulk letter and each document separated in my database.

My merged bulk letter method worked perfectly, but how can I all the single documents in the bulk to save separated in my database.

The doc.MailMerge.Execute(datarow) methode is not possible for me, I have 25 Tables in my DataSet and I think that is not possible through every row to go and to call the method to merge.

-> My DataSet:
customer -> addresses
employee -> addresses
contracts -> products
invoices -> …

Hi

Thanks for your inquiry. Please follow the link to learn how to produce multiple documents upon mail merge:
https://docs.aspose.com/words/net/mail-merge-and-reporting/
Also, I think the following article could be useful for you:
https://docs.aspose.com/words/net/serialize-and-work-with-a-document-in-a-database/
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.

Your Solution explained me, how I can merge a DataRow from one DataTable.

I know how I can save to database, and I know it too how I can merge data from one DataTable. But my problem is, I would like merge from a DataSet with some Relations, row to row for all Ids in the DataSet Result. Just as the Method “ExecuteWithRegions” but for all documents apart.

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?

Yes, thats my Goal. One mail merge and each child row (over 25tables with contraints from my dataset) to documents.

If I have 10 rows would I get 10 documents for each row and 1 mail merge. Altogether that makes 11 documents for save or whatever…

If you think that the link from alexey that is what I need, is this possible that you post a codesnippet here, there solves my problem? (NOT only 1 Table, but rather over 25 Tables)

DataSource
1 DataSet with 25 Tables

All Tables with Relations among each other

Output Requirement
1 nested mail merge

n nested single documents

Thanks for help!

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.
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,

Thank you for help, but that doesn’t work for me.

Working over 36 hours at this problem and I’m frustrated…
I’m wondering why it doesn’t possible to define the output over a configuration.

eg.

Document[] documents = doc.MailMerge.ExecuteWithRegions(_dokumentMergeDataSet, OutputParam.SingleDocuments);
foreach(var document in documents)
{
    doc.Save(document, SaveParameter.Doc);
}

that would something simple…

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

My Goal:
1 Merged Document (attached)
5 Single Documents per contract (german - vertrag)

(It matters that all must be generic, i don’t know any names from the datatables or columns in there!)

Thanks

Hi

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:
    https://docs.aspose.com/words/net/mail-merge-and-reporting/

Best regards,

Hi,

I’ve solved the problem.
Thanks for your help.

Best regards biggle