Similar question - Position cursor at start/end of Word document
这个答案进入有关which
和what
更多细节。答案是c#和vb之间的混搭,所以我在这里包含还有另一个答案,它使用稍微不同的方法去到文档的最后部分。
我的两分钱:
//vb does this kind of thing for them
//but in c# we need an object we can pretend is null
object oMissing = System.Reflection.Missing.Value;
//Start Word and create a new document.
Word._Application oWord;
Word._Document oDoc; //whenever i read this i think 'hodor'
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object StartPos = 0;
object Endpos = 1;
Microsoft.Office.Interop.Word.Range rng = oDoc.Range(ref StartPos, ref Endpos);
rng.Text = "This is first line Word from C#";
//object what = Word.WdGoToItem.wdGoToLine;
//I couldn't get wdGoToLine to work but wdGoToPercent was happy
object what = Word.WdGoToItem.wdGoToPercent;
object which = Word.WdGoToDirection.wdGoToLast;
oWord.Selection.GoTo(ref what, ref which, oMissing, oMissing);
这种方法,因为它并没有告诉字将光标移动到最后一行略有不同,但最后文档,百分比我会必须假定为100.这将是单行文档中行的结尾,但如果光标位于第一行(在开始处),并且我们告诉Word转到最后一行,则不会发生任何事情:我们是已经在那里,开始的最后一行。
它似乎没有工作。 – Rohit