Page tree
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 12 Next »

Table of Contents

Intro

Icon

This tutorial assumes a basic understanding of the WordTemplate object and behavior when importing multiple rows of data. If you are not familiar with these concepts, please go through the Sales Invoice tutorial first.

The WordTemplate object is used for template-driven document generation. This object opens a WordWriter template file, populates it with data from a specified data source, and generates a new Word document. The WordTemplate object provides the ability to import multiple rows of data by repeating sections of a document for each row, or using the entire document as a repeatable section.

This tutorial covers how to use WordTemplate.SetMailMerge to use the entire document as a repeatable section and then how to use NEXT fields to populate a sheet of business labels, over multiple pages.

Business Label Template

Following the Sample Code

Icon

In the downloadable WordWriter_Basic_Tutorials.zip, there is a completed template file located in BusinessLabels/templates/Business_Label_Template.docx.

The template file looks like this:

There is a table with fields for an employee's FirstName, MI, LastName, Phone, Email, Street, City, State, and ZipCode. These fields are repeated for an entire page in the document. The goal is to populate all of the business labels, but spill onto multiple pages as necessary.

Unlike with repeat blocks that are populated with WordTemplate.SetRepeatBlock, which require bookmarks to determine what part of the document is repeated, WordTemplate.SetMailMerge treats the entire document as a repeat block, so it is not necessary to add bookmarks when using SetMailMerge.

Adding a WordWriter Reference in Visual Studio

Following the Sample Code

Icon

In the sample code, the reference to SoftArtisans.OfficeWriter.WordWriter.dll has already been added to the BusinessLabels web application project.

To create a .NET project and add a reference to the WordWriter library:

  1. Open Visual Studio and create a .NET project.
  2. Add a reference to the SoftArtisans.OfficeWriter.WordWriter.dll
    • SoftArtisans.OfficeWriter.WordWriter.dll is located under Program Files > SoftArtisans > OfficeWriter > dotnet > bin.

Writing the Code

Following the Sample Code

Icon

The code for this tutorial can be found under BusinessLabels/Part1.aspx.cs.

This part of the tutorial will cover how to set up the WordTemplate code to bind the data to the template. This assumes a basic knowledge of WordTemplate and how to bind data to Word templates.

1. Include the SoftArtisans.OfficeWriter.WordWriter namespace in the code behind

2. Create a new WordTemplate object and open the template file.

3. Get the employee data for the business labels.

Following the Sample Code

Icon

In the sample project, we are parsing CSV files with query results, rather than querying a live database. The CSV files are available under the data directory. There is a copy of the CSV parser, GenericParsing.dll in the bin directory of the project. The helper method GetCSVData is defined in Part1.aspx.cs in a region marked Utility Methods.

This call is to a helper method GetCSVData that parses the CSV files and returns a DataTable with the values.

4. Use WordTemplate.SetMailMerge to bind the employee data to the template.

Recall that SetMailMerge treats the entire document as a repeat block. This means that the merge fields in the document will need to match the column names of the data source. If there are any stray merge fields in the document, WordWriter will throw an error.

5. Process and save the template file.

The completed code should look like this:

Run the code. You will notice that each employee has a page of business label's with that particular employee's data.

This is the intended SetMailMerge behavior, as the entire document was repeated for each row of data. In the next section we will incorporate next fields so that only one business label is made for each employee.

Adding NEXT fields

Final Code

Downloads

  • No labels