Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
 private string returnName, returnStreet, returnCityStateZip,
                    recipName, recipStreet, recipCityStateZip;
        /// <summary>
        /// Build the report with WordTemplate
        /// </summary>
        public void GenerateDocument()
        {
            //Set the recip and return adresses to the desired adresses:
            returnName = "Jon Smith";
            returnStreet = "123 Main St";
            returnCityStateZip = "Springfield, WA 05555";
 
            recipName = "Robert Jones";
            recipStreet = "321 Orange Rd.";
            recipCityStateZip = "Greenville, IL 04444";
 
 
            // 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[] arrNames = {"RtnName", "RtnStreet", "RtnCityStateZip",
                                "RcpName", "RcpStreet", "RcpCityStateZip"};
 
            // The value array.  The value of the
            // elements in this array contain info supplied in the web form.
            // These values represent the data that will be dynamically populated
            //in the template file
           
            object[] arrValues = {this.returnName, this.returnStreet, this.returnCityStateZip,
                                     this.recipName, this.recipStreet, this.recipCityStateZip};
 
            // Instantiate the WordTemplate object
            WordTemplate wt = new WordTemplate();
 
            // Open the template file
            string templatePath = @"..\..\WordTemplateFiles\EnvelopeTemplate.docx";
            wt.Open(templatePath);
 
            // Set the datasource with the name and value arrays defined above */
            wt.SetDataSource(arrValues, arrNames);
 
            // Process the template
            wt.Process();
 
            // Save the document to the desired location
            wt.Save(@"..\..\WordOutputFiles\Envelope_output.docx");
        }
 
    }
 

Downloads

Panel

Template:  EnvelopeTemplate.docx

Output:  Envelope_output.docx

 
\\\