This is a basic demo that inserts data into a template document that has two merge fields.

Enter values into the text boxes below and click the button.  The values will be inserted into the template document by WordTemplate and streamed back to you for viewing in MSWord.

If you are just learning WordTemplate, this demo is a good place to start looking at the code.  The general sequence of handling a WordTemplate document is as follows:

  1. Create an instance of WordTemplate
  2. Open the template document
  3. Set one or more document data sources or repeat blocks
  4. Call the WordTemplate.Process() method to pull in the data
  5. Save the document either to disk, or to a response stream to the client browser.


        private string recipName;
        private string recipCompany;

        /// <summary>
        /// Build the report with WordTemplate
        /// </summary>
        public void GenerateDocument()
            this.recipCompany = "SoftArtisans";
            this.recipName = "Jon Smith";
            // Form the array of mergefield names.  The elements in this array
            // correspond to the names of the merge fields in the template,
            // and each element's array index should correspond to an element in the
            // value array

            string[] NamesArr = { "Name", "Company", "DateTime" };

            //Form an array containg the values to be inserted
            object[] ValuesArr = { recipName, recipCompany, System.DateTime.Now };

            //Create an instance of WordTemplate
            WordTemplate wt = new WordTemplate();

            //Open the template document
            string templatePath = @"..\..\WordTemplateFiles\BasicTemplate.docx";

            //Set the main data source with the Name and Value arrays
            wt.SetDataSource(ValuesArr, NamesArr);

            //Populate the template to pull in the new values

            //Save the populated document




 Template: BasicTemplate.docx

 Output:  BasicTemplate_output.docx