C# restart list numbering after copying "Template-Pages" in word

31 Views Asked by At

I'm trying to create some kind of mail merger algorithm. everything works as I expect it but I've some problems with the nubmering scheme from word 2016.

The algorith should work like this:

  • load a prepared word file (could contain any kind of text, numbered lists or bullet lists, several number of pages)
  • copy this template
  • fill up some predefined fields (for example address field)
  • past the "template pages" at the document end
  • fill up the fields with other values
  • ...

My problem is, that word will allways continue the listnumbering and not restarting it at the end of the "template" pages. Have you any idea?

{
    Stopwatch stopWatch = new Stopwatch();
    stopWatch.Start();

    //CREATING OBJECTS OF WORD AND DOCUMENT
    oWord = new Word.Application();
    oWordDoc = new Word.Document();
    //MAKING THE APPLICATION VISIBLE
    oWord.Visible = false;

    //ADDING A NEW DOCUMENT FROM A TEMPLATE
    oWordDoc = oWord.Documents.Add(ref Template, ref oMissing, ref oMissing, ref oMissing);
    

    // Prepare default field values
    InsertTextInField(oWordDoc.Fields, "City", _city);


    // Get the page count and optimize for duplex printing if template has odd page count.
    var numberOfPages = oWordDoc.ComputeStatistics(WdStatistic.wdStatisticPages, false);
    if (_duplex)
    {
        if ((numberOfPages % 2) > 0)
        {
            oWord.Application.Selection.EndKey(Word.WdUnits.wdStory);
            oWord.Application.Selection.InsertNewPage();
        }
    } 


    // Get "default" page for later
    Word.Range oRange = oWordDoc.Content;
    oRange.Copy();            


    // deal with first maling page issue and add a blank page at the end
    if (_genericMail.Count > 1)
    {
        oWord.Application.Selection.EndKey(Word.WdUnits.wdStory);
        object breakType = Word.WdBreakType.wdPageBreak;
        oWord.Application.Selection.InsertBreak(ref breakType);
    }


    for (int i = 0; i < _genericMail.Count; i++)
    {
        //InsertTextInField(oWordDoc.Fields, "Address", _genericMail[i].Label.ToString());
        FillAddressLabel(i + 1, _genericMail[i].Label.ToString());

        oWordDoc.Range(oWordDoc.Content.End - 1, oWordDoc.Content.End - 1).Paste();
        if (i < (_genericMail.Count - 1))
        {
            oWordDoc.Range(oWordDoc.Content.End - 1, oWordDoc.Content.End - 1).InsertBreak(Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak);
        }

        int progress = 100 * i / _genericMail.Count;
        _backgroundWorker.ReportProgress(progress);
    }

    oWord.Visible = true;
}

Restarting line numbering with C# at defined page ends.

0

There are 0 best solutions below