TX Text Control Source Code Library

Mail Merge: Creating and Merging MS Word Compatible Templates

Mail Merge DesignerMail merge is the most typical application for TX Text Control. The combination of TX Text Control's MS Word compatible field functionality and TX Text Control Server for ASP.NET (incl. Windows Forms)'s automated MailMerge component is a very powerful toolset to build high peformance and MS Word compatible mail merge applications.

TX Text Control supports not only the merge process, but also provides the required editor functionality to create templates with a true WYSIWYG interface.

This sample project shows most of the mail merge possibilities of TX Text Control. The following steps are included:

  • Database binding
  • Adding fields
  • Preview
  • Export the merged document

It is quite similar to MS Word mail merge capabilities, but it comes with source code and can be customized completely. The following tutorial explains how to use this sample.

  1. Connect to a database
    • Click on the Select recipients button in the toolbar or choose Select Recipients... from the Mail Merge menu.
      Selection recipients

    • In the opened dialog box, browse for the shipped XML database file address.xml which is located in the sample root folder and load it by clicking Open.
    Now you can see the contained DataTables in the drop down box of the toolbar. If the database would contain more DataTables, you could select the required DataTable from this box.
    Based on the selected database and DataTable, the contained merge fields are automatically added to the Insert Field drop down button and the Insert Merge Field menu item.
    Merge fields

  2. Insert merge fields
    • Insert some merge fields from the above filled drop down list. These fields will be inserted at the current input position.
      Added fields
    • You can switch the view mode of the fields using these three buttons in the toolbar:
      Field view modes
      • Field code: This view mode shows the MS Word field codes of the field.
      • Field text: This view mode shows the results or the field name.
      • Preview: This view mode merges the database content into the specific fields.
    • Click on the Preview button. The second database navigator toolbar gets enabled and you can navigate through the database in order to preview your template with the real data:
      Merge preview
  3. Export the document
    • Finally, click the Export button to start the mail merge process:
      Export button
      This opens a new form that consists of a TX Text Control and a simple toolbar. The DocumentServer.MailMerge component is used in the background to merge the created template with the data records from the selected database. The resulting document is copied into the visual TX Text Control instance on the form. The actual merge process is done in 3 lines of code:
      mailMerge.TextComponent = tx; mailMerge.LoadTemplateFromMemory(Document, TXTextControl.DocumentServer.FileFormat.InternalUnicodeFormat); mailMerge.Merge(DataTable, true);
      Exported document

The minimum requirements for this sample application are TX Text Control Server for ASP.NET (incl. Windows Forms) 15.1 trial version and Visual Studio 2008.

 
 

Products

Support

Downloads

Corporate

Buy Now