在Word互操作中使用数组中的for循环
本文关键字:for 循环 数组 Word 互操作 | 更新日期: 2023-09-27 17:54:25
我应该在每个循环中插入文本到新页面。每次我通过rng.Text = array[i].ToString()
插入文本,它都很顺利,但是数组的第一项插入到最后一页并且对齐不良。
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);
SOLVED.
在第一次迭代中(通过if(i == 0)),我放了另一个for,它是通过count of array
for (int m = 0; m < countFor-1; m++)
{
document.Words.Last.InsertBreak(Word.WdBreakType.wdPageBreak);
}
这段代码在数组项的计数中添加页,并确保页的顺序是正确的。我不知道它是如何工作的,但它是功能性的解决方案。