TX Text Control .NET Documentation

Sample: Mail Merge with Repeating Blocks

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.

[C#] 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.MergeBlocks(dsAddresses); 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.MergeBlocks(dsAddresses) mailMerge1.Merge(dsAddresses.Tables("address"), True)

Continue with the next sample

Call us: +1 704-544-7445 (United States)





Buy Now

About Text Control

Founded in 1991, Text Control is an award-winning Visual Studio Industry Partner and leading vendor of word processing and reporting components for Microsoft development technologies. The products help thousands of developers add comprehensive word processing functionality to their applications. Text Control is 100% self-owned and independent of all investor decisions. Customers benefit from these years of experience, large user-base, and at the same time, appreciate developing with a mature, reliable product.

SD Times 100SD Times 100SD Times 100SD Times 100SD Times 100SD Times 100Visual Studio 2015 Launch PartnerVisual Studio 2013 Launch PartnerVisual Studio 2012 Launch PartnerVisual Studio Partner