MS Word Mail Merge V9 Advice

I'm currently using a C# script to generate a word document and populate it with some data. I have a visual script for each record loop that needs to create a new page in the same document and add more data to it. I know very little about C# so wondered if someone could give me some advice.

 

With my existing code it just overwrites the data with the last record in the loop.

 

Any help would be much appreciated.

 

This is my current code :-

 

#region

Using Statement

 

using

System;

using

System.Data;

using

System.Collections.Generic;

using

Metastorm.Ide.Extensibility;

using

Metastorm.Runtime.Core;

using

Metastorm.Runtime.Types;

using

System.ComponentModel;

 

using

System.IO;

using

DocumentFormat.OpenXml.Packaging;

 

#endregion

Using Statement

 

//Change this to a namespace that will suit you

namespace

Metastorm.Runtime.Models.Staff_Parking_Pemits

{

 

///

  /// This needs to be a class or an interface that will support public, static methods/properties   ///

 

publicclass WordExample

  {

       

// To search and replace content in a Word document.

       

publicstaticvoid SearchAndReplace(string wordTemplatePath, string wordOutputPath, string findString, string replaceString, string findString2, string replaceString2, string findString3, string replaceString3, string findString4, string replaceString4, string findString5, string replaceString5, string findString6, string replaceString6, string findString7, string replaceString7, string findString8, string replaceString8, string findString9, string replaceString9, string findString10, string replaceString10, string findString11, string replaceString11, string findString12, string replaceString12)

//string findString5, string replaceString5, string findString6, string replaceString6)

        {

 

           

//create instance of our template in the output location

            File.Copy(wordTemplatePath, wordOutputPath,

true); // overwrite = true

           

using (WordprocessingDocument wordOutputDoc = WordprocessingDocument.Open(wordOutputPath, true))

            {

               

string docText = null;

               

using (StreamReader sr = new StreamReader(wordOutputDoc.MainDocumentPart.GetStream()))

                {

                    docText = sr.ReadToEnd();

                }

                docText = docText.Replace(findString, replaceString);

docText = docText.Replace(findString2, replaceString2);

docText = docText.Replace(findString3, replaceString3);

docText = docText.Replace(findString4, replaceString4);

docText = docText.Replace(findString5, replaceString5);

docText = docText.Replace(findString6, replaceString6);

docText = docText.Replace(findString7, replaceString7);

docText = docText.Replace(findString8, replaceString8);

docText = docText.Replace(findString9, replaceString9);

docText = docText.Replace(findString10, replaceString10);

        docText = docText.Replace(findString11, replaceString11);

docText = docText.Replace(findString12, replaceString12);

 

               

using (StreamWriter sw = new StreamWriter(wordOutputDoc.MainDocumentPart.GetStream(FileMode.Create)))

                {

                    sw.Write(docText);

                }

            }

        }

 

 

 

  }

}

Tagged:

Comments