Execute mail merge for a multi-page template - each page with different data source

Hello there,

I have a 4-page template for which I need to run mail merge.
But for each page, I need to call Execute(ds) or ExecuteWithRegions(ds)… each page with different data source.

Could you please guide me the right approach to achieve above.

Thank you!

Hi

Thanks for your request. You can use mail merge with regions only if there are regions in your document, i.e. TableStart and TableEnd mergefields. If there are no regions in your template, then you should use simple mail merge.
Best regards.

Thanks Alexey,

I think I didn’t put my query in right way. I will try to reframe my words:

Actually my source template is of 4 pages and you know when we run MailMerge.Execute(ds), ds is data source for mail merge fields, we run it for the whole document.

But the issue in my case is each page in the source Template has to have a different data source.

Apart from splitting the template to four different template and then run Mail Merge for each of them,
what would be the possible way to achieve above?

Hi

Thank you for additional information. I think you have only two options to achieve this.

  1. You can split your template into separate documents, execute simple mail merge on each of these templates and then concatenate output documents into one final document.
  2. You can use regions for each of page in your original template and execute mail merge with regions.

Also, could you please attach your template here for testing? I will take a look on it and provide you more information.
Best regards.

Thanks for your reply.

But 2nd option doesn’t seem feasible for my data source.

Please find attached the sample template.
FYI,
Fields starting with P belong to Property
Fields starting with A belong to Applicant (Applicants is a child collection of Property object) relation- 1:M
Fields starting with V belong to Vendor (Vendors is a child collection of Property object) relation- 1:M
Fields starting with T belong to Vendor’s Solicitor relation- 1:1

Example Scenerio:
If this template is called for a
Property which have, say –
- 2 Vendors, say V1, V2
- 3 Applicants, say A1, A2, A3
The output document should be generated as follows:

According to template –
- Because 1st page is Solicitor oriented, so should be in output once (as there is only one vendor’s Solicitor that may be associated to a Property)
- 2nd page is Vendor oriented, so should be outputted twice, one for each Vendor
- 3rd page is Applicant oriented, so should be outputted thrice, one for each Applicant
- 4th page is Vendor oriented, so should be outputted twice, one for each Vendor
Total no. of pages in final mail merge output - 8

Hi

Thank you for additional information. As I can see each of pages has fields of different entities in your data source. For example, the first page contains fields of Solicitors, Properties and Vendors.
I think, you should use the first approach I suggested earlier, i.e. split your template into few documents and fill them separately. Then you can just concatenate documents.
However, if you would like to use the second approach, i.e. using regions, I think you should also perform preprocessing of your data source in order to get data sources for each region, which contains all required fields within the region.
Best regards.

Thank you Alexey for your help.

Even I am thinking of using the second approach or we might suggest user to upload such templates after splitting them.

Thanks again!

Hey Alexey,

Is there any way I can split my source template to multiple documents at run-time.

May be on the basis of some codes available in the source document like:

<Solicitor>
...
</Solicitor>
<Vendor>
...
</Vendor>

So that I will have two templates at run time, one for Solicitor and other for Vendor.

Is there any Aspose method available to split a source document like above?

Thank you!

Any comments on this?

Thank you!

Hi

Thanks for your request. There are many examples how to split document in the forum. For example, you can try using the approach described in the following thread:
https://forum.aspose.com/t/99344
Hope this helps.
Best regards.

Thanks Alexey,

splitting my document to multiple documents is working fine using the way mentioned in above thread.

However all my resulting documents are being appended with empty page.

Any idea how can I avoid this extra page?

I am using exactly the same code as mentioned in above thread, except last else part:

public List SplitDocument(string separator)
{
    // Create regex
    Regex regex = new Regex(Regex.Escape(separator));
    mDoc.Range.Replace(regex, new ReplaceEvaluator(ReplaceGetSeparator), false);

    // Create new Documents list
    List subDocuments = new List();

    // Create sub document
    Document subDoc = null;

    // Loop through all sections in the document
    foreach(Section sect in mDoc.Sections)
    {
        // Loop through all nodes in section
        foreach(Node node in sect.Body.ChildNodes)
        {
            if (mSeparators.Contains(node))
            {
                // Add sub document to the collection
                if (subDoc != null)
                    subDocuments.Add(subDoc);

                // Create new sub document
                subDoc = new Document();
                subDoc.FirstSection.Body.RemoveAllChildren();
            }

            else if (mDoc.LastSection.Body.LastChild.Equals(node))
            {
                // Import node and insert it into sub document
                Node dstNode = subDoc.ImportNode(node, true, ImportFormatMode.KeepSourceFormatting);
                subDoc.FirstSection.Body.AppendChild(dstNode);

                // Add sub document to the collection
                if (subDoc != null)
                    subDocuments.Add(subDoc);
            }

            else
            {
                // Import node and insert it into sub document
                if (subDoc == null)
                {
                    subDoc = new Document();
                }
                Node dstNode = subDoc.ImportNode(node, true, ImportFormatMode.KeepSourceFormatting);
                subDoc.FirstSection.Body.AppendChild(dstNode);
            }
        }
    }

    return subDocuments;
}

// Have added additional if condition (in bold) to initialize subDoc, otherwise was throwing errror: Object Reference not set to an instance...!

Thank you!

P.S. Source document is attached herewith.

Hi

Thank you for additional information. Could you please also attach your intermediate document and final document here? I will check the issue and provide you more information.
Best regards.

Thanks Alexey,

Here are the multiple output docs that I get after splitting the source document(attached earlier).
You will notice that each splitted page has an extra page appended to it.

Please guide how can I avoid that.

Thanks a lot!

Hi

Thank you for additional information. Extra pages are added because there are empty paragraphs at the end of your subdocuments. You can try deleting empty paragraphs from the end of the document. Please see the following code for instance:

Document doc = new Document(@"Test001\out_1.doc");
while (!doc.LastSection.Body.LastParagraph.HasChildNodes)
    doc.LastSection.Body.LastParagraph.Remove();
doc.Save(@"Test001\out.doc");

Also, in the first document there is PageBreak, that is why an empty page occurs at the end of the document.
Best regards.