'How to write the desired first line on each sheet word document C#

How can I insert text into a Word document (use C# Interop) so that the first line of each sheet has an inscription, for example "continuation of the table"? I suppose to do it like this...

Find the lines of each of the pages and on each page find the first line and insert. Maybe it can be done differently ... more gracefully. But even with my approach, not all lines are displayed.

var rng = docDocument.Range();
int LineCount = rng.ComputeStatistics(Word.WdStatistic.wdStatisticLines);

try
{
    for (int ii = 1; ii < LineCount; ii++)
    {
        CurrentPageNumber = (Convert.ToInt32(ii.ToString()));
        NextPageNumber = (Convert.ToInt32((ii + 1).ToString()));

        // Get start position of current page
        Start = oWord.Selection.GoTo(ref What, ref Which, ref CurrentPageNumber, ref Miss).Start;

        // Get end position of current page
        End = oWord.Selection.GoTo(ref What, ref Which, ref NextPageNumber, ref Miss).End;

        var rng_page = docDocument.Range(Start, End);
        string textLine = rng_page.Text;
        Console.WriteLine(textLine);

        var line_rng = docDocument.Range(unit);
        string textline_line = line_rng.Text;
        //Console.WriteLine(textline_line);
    }
}
catch (Exception ex)
{
    _ = ex.Message;
}

It looks like something has moved... But each new page has one or two new lines. In addition, since the file being edited has a large number of sheets, which entails additional lines and additional sheets, and this function is no longer performed on these sheets.

for (int Index = 2; Index <= PagesCount; Index++)
{
    CurrentPageNumber = (Convert.ToInt32(Index.ToString()));
    Word.Range range1 = docDocument.GoTo(ref objWhat, ref objWhich, CurrentPageNumber, ref Miss);
    Word.Range range2 = range1.GoTo(Word.WdGoToItem.wdGoToLine);
    object objStart = range1.Start;
    object objEnd = range2.End;
    //string str = docDocument.Range(ref objStart, ref objEnd).Text;
    range1.InsertParagraph();
    range1.Text = "new_TEXT_new_TXET\n";

}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source