2014-09-25 57 views
1

我需要更改目錄的範圍,以便我可以在文檔的第二頁上啓動。任何人都可以幫助我設置範圍?設置範圍在文檔的第二頁上啓動

下面的代碼是我現在有的範圍,但是這將在word文檔的乞求中生成目錄,我需要將它插入第二頁。

object start = oWord.ActiveDocument.Content.Start; 
Word.Range rangeForTOC = oDoc.Range(ref oMissing, ref start); 

這就是我與測試此:

object gotoPage1 = Word.WdGoToItem.wdGoToPage; 
object gotoNext1 = Word.WdGoToDirection.wdGoToAbsolute; 
object gotoCount1 = null; 
object gotoName1 = 1; 

oWord.Selection.GoTo(ref gotoPage1, ref gotoNext1, ref gotoCount1, ref gotoName1); 

//Insert a blank page 
oWord.Selection.InsertNewPage(); 
oWord.Selection.InsertNewPage(); 

object what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage; 
object which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToAbsolute; 
object count = 2; //change this number to specify the start of a different page 

oWord.Selection.GoTo(ref what, ref which, ref count, ref oMissing); 


Object beginPageTwo = oWord.Selection.Range.Start; 
// This gets the start of the page specified by count object 

Word.Range rangeForTOC = oDoc.Range(ref oMissing, ref beginPageTwo); 

object oTrueValue = true; 
+0

嘗試在第一頁的末尾插入分頁符。 – user3165438 2014-09-28 09:23:01

回答

2

這裏是你如何能做到這一點:

object missing = System.Reflection.Missing.Value; 

object what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage; 
object which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToAbsolute; 
object count = 2; //change this number to specify the start of a different page 

oWord.Selection.GoTo(ref what, ref which, ref count, ref missing); 
Object beginPageTwo = oWord.Selection.Range.Start; // This gets the start of the page specified by count object 

Word.Range rangeForTOC = oDoc.Range(ref beginPageTwo); //modified this line per comments 

上面代碼中包含代碼SO - how can we open a word file with specific page number in c sharp?

測試用來驗證這是基於註釋工作代碼:(編輯)

object fileName = (object)@"C:\test.docx"; 
object oMissing = System.Reflection.Missing.Value; 

Microsoft.Office.Interop.Word.Application oWord = new Application(); 
oWord.Documents.Open(ref fileName); 

object gotoPage1 = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage; 
object gotoNext1 = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToAbsolute; 
object gotoCount1 = null; 
object gotoName1 = 1; 

oWord.Selection.GoTo(ref gotoPage1, ref gotoNext1, ref gotoCount1, ref gotoName1); 

//Insert a blank page 
oWord.Selection.InsertNewPage(); 

object what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage; 
object which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToAbsolute; 
object count = 2; //change this number to specify the start of a different page 

oWord.Selection.GoTo(ref what, ref which, ref count, ref oMissing); 

Object beginPageTwo = oWord.Selection.Range.Start; // This gets the start of the page specified by count object 

Microsoft.Office.Interop.Word.Range rangeForTOC = oWord.ActiveDocument.Range(ref beginPageTwo); 

oWord.ActiveDocument.TablesOfContents.Add(rangeForTOC); 

我測試此代碼對Word 2010中使用Visual Studio 2012的高級目標的.NET Framework 4.0。

+0

您可能需要將FYI中的變量名稱與您的特定代碼進行匹配。 – jordanhill123 2014-09-25 01:24:58

+0

我真的很清楚爲什麼,但是這在第一頁上生成的仍然是.... :(@ jordanhill123 – sharpiee 2014-09-25 02:10:40

+0

如果您運行的樣本測試代碼用於驗證它是否工作正常,是否適用於您? – jordanhill123 2014-09-25 13:59:08