Section break (with page break) using mail merge

I need to build a document (letters for a lot of recipients) with the following conditions:

  1. there is a template for one recipient of the letter
  2. the template consists of more than 1 page
  3. when using mail merge from microsoft word the template would be duplicated and seperated by section breaks (including page breaks). This way page numbering could start by 1 for every recipient.

Unfortunately a section break cannot be between TableStart and TableEnd Tag in Aspose Words. When using MailMergeWithRegions there are no section breaks inside the resulting document. Therefore the page numbers are not reset for a new recipient (btw. the page numbers are not the only problem when the section breaks are missing).

Now the question: how can one use MailMerge (with Regions) and get a section break after each master record from the datasource (=every recipient of the letter).

Thanks in advance

Mario

Hi Mario,

Thanks for your inquiry.

*mario.kuegow:

  1. when using mail merge from microsoft word the template would be duplicated and seperated by section breaks (including page breaks). This way page numbering could start by 1 for every recipient.*

To restart the page numbering at the start of section the PageSetup.RestartPageNumbering property must be set to true. The number which this is restarted to is defined by the PageSetup.PageStartingNumber property.
This property is set to “1” in Microsoft Word and Aspose.Words by
default. For more details, I would suggest you please read the following
article:
https://docs.aspose.com/words/net/working-with-fields/

*mario.kuegow:

Unfortunately a section break cannot be between TableStart and TableEnd Tag in Aspose Words. When using MailMergeWithRegions there are no section breaks inside the resulting document. Therefore the page numbers are not reset for a new recipient (btw. the page numbers are not the only problem when the section breaks are missing).
Now the question: how can one use MailMerge (with Regions) and get a section break after each master record from the datasource (=every recipient of the letter).*

Yes, TableStart and TableEnd fields must be inside the same section in the document. In your case, I suggest you following solution.

  1. Implement IFieldMergingCallback interface, track for each occurrence of the last merge field inside your ‘base’ region and insert a temporary Bookmark (e.g bookmark name start with ‘bm_’) .

  2. After calling the ExecuteWithRegions method, insert a Section Break
    at the places where you inserted temporary Bookmarks in step 1 and
    after that remove those temporary Bookmarks from document: Please see
    the following code snippet:

…
...
doc.MailMerge.FieldMergingCallback = new HandleMergeField();
doc.MailMerge.ExecuteWithRegions(ds);
DocumentBuilder builder = new DocumentBuilder(doc);
foreach (Bookmark bm in doc.Range.Bookmarks)
{
    if (bm.Name.StartsWith("bm_"))
    {
        builder.MoveToBookmark(bm.Name);
        builder.InsertBreak(BreakType.SectionBreakNewPage);
    }
}
doc.Range.Bookmarks.Clear();

If you still face problem, please share following detail for investigation purposes.

  • Please attach your input Word template document.

  • Please create a standalone/runnable simple application (for example a Console
    Application Project) that demonstrates the code you used to generate your output document

  • Please attach the output Word file that shows the undesired behavior.

  • Please attach your target Word document showing the desired behavior. You can
    use Microsoft Word to create your target Word document. I will investigate as to how you are expecting your final document be generated like.

Thank you very much!
It works great.

Best Regards

Mario

Hi Mario,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.