Using array in for cycle in Word Interop

115 views Asked by At

I should insert text to new page every each cycle. Every each I insert text by rng.Text = array[i].ToString() and it´s gone well, but first item of array is inserted on the last page and with bad alignment.

object what = Word.WdGoToItem.wdGoToPage;
object which = Word.WdGoToDirection.wdGoToAbsolute;
object count = null;

//countFor is count of items in my array
for (int i = 0; i < countFor; i++)
{
    count = i;
    object range = winword.Selection.GoTo(ref what, ref which, ref count, ref oMissing);
    object beginPageTwo = winword.Selection.Range.Start;
    object beginNextRng = winword.Selection.Range.StartOf();
    object end = winword.Selection.Range.End;

    Word.Range rng = winword.ActiveDocument.Range(beginPageTwo, end);
    Word.Range rng2 = winword.ActiveDocument.Range(beginPageTwo, end);
    Word.Range rng3 = winword.ActiveDocument.Range(beginPageTwo, end);
    Word.Range rng4 = winword.ActiveDocument.Range(beginPageTwo, end);
    Word.Range rng5 = winword.ActiveDocument.Range(beginPageTwo, end);
1

There are 1 answers

0
Dominik Zatloukal On

SOLVED.

In first iterration (by if(i == 0) ) I put another for which is going through count of array

for (int m = 0; m < countFor-1; m++) { document.Words.Last.InsertBreak(Word.WdBreakType.wdPageBreak); }

This code add pages in count of items in array and ensure that the order of pages wil be okay. I don´t know, how it work, but it´s functional solution.