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