在Word文档的开头插入目录

时间:2014-09-24 22:10:38

标签: c# .net ms-word office-interop tableofcontents

我的目录代码如下。我需要把它放在我文档的第二页上。该文件长达15页。要将其插入第二页,我必须在第一页的末尾添加分页符,然后在第二页上插入目录。

如何将其放在文档的第二页?我知道它有范围的东西,但我要注意如何做到这一点。

目录的代码如下。

object oTrueValue = true;
        object start = oWord.ActiveDocument.Content.End - 1;

        Word.Range rangeForTOC = oDoc.Range(ref start, ref oMissing);
        Word.TableOfContents toc = oDoc.TablesOfContents.Add(rangeForTOC, ref oTrueValue, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oTrueValue);
        toc.Update();

        Word.Range rngTOC = toc.Range;
        rngTOC.Font.Size = 12;
        rngTOC.Font.Name = "Arial";
        rngTOC.Font.Bold = 0;

1 个答案:

答案 0 :(得分:1)

转到第一页的末尾,然后添加分页符,然后添加ToC:

// Go to end of document
Object what = WdGoToItem.wdGoToLine;
Object which = WdGoToDirection.wdGoToLast;
wordApp.Selection.GoTo(what, which, ref missing, ref missing);  

插入分页符:

`selection.TypeText("\f");//page break`

创建ToC:

selection.Font.Bold = 1;
selection.TypeText("Table of Content\n");
TableOfContents toc = wordDoc.TablesOfContents.Add(selection.Range, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
}//if