Sample: Mail Merge with Repeating Blocks

Windows Forms User's Guide > Howto: Mail Merge

The DocumentServer.MailMerge component can not only merge single fields in a template. Additionally, blocks can be defined that are repeated based on the number of specified data rows.

A MergeBlock is a region in a document that will be repeated in the merge process. This block can be easily inserted using the shipped TX Text Control Words. The document element SubTextPart range to define the beginning and the end of a repeating block. SubTextParts have a special naming, so that TX Text Control's MailMerge class can find the blocks and connected data. The name of the SubTextPart begins with txmb_ + the name of the merge block.

Load the sample program from the MailMerge_Blocks directory and start it. Automatically, a template is loaded that consists of simple merge fields and a repeating block which is basically the second row of the table.

Choose Merge from the MailMerge main menu to start the merge process. With a very few lines of code, the template is loaded and the data is merged using the DocumentServer.MailMerge component.

private DataSet dsAddresses;

dsAddresses = new DataSet();
dsAddresses.ReadXml(Application.StartupPath + "\\data.xml");

TXTextControl.LoadSettings ls = new TXTextControl.LoadSettings();
ls.ApplicationFieldFormat = TXTextControl.ApplicationFieldFormat.MSWord;
ls.LoadSubTextParts = true;

textControl1.Load(Application.StartupPath + "\\template.docx", TXTextControl.StreamType.WordprocessingML, ls);

mailMerge1.Merge(dsAddresses.Tables["address"], true);
[Visual Basic]
Private dsAddresses As DataSet

dsAddresses = New DataSet()
dsAddresses.ReadXml(Application.StartupPath + "\data.xml")

Dim ls As New TXTextControl.LoadSettings()
ls.ApplicationFieldFormat = TXTextControl.ApplicationFieldFormat.MSWord
ls.LoadSubTextParts = True

textControl1.Load(Application.StartupPath + "\template.docx", TXTextControl.StreamType.WordprocessingML, ls)

mailMerge1.Merge(dsAddresses.Tables("address"), True)