c#仅在当前部分添加页脚

时间:2018-04-09 19:34:04

标签: c#

我正在使用c#和Microsoft.Office.Interop.Word;。

这是我用来浏览文档的每个部分并在单击按钮时添加页脚的for循环。

private void btnOK_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Word._Application oWord;
            object oMissing = Type.Missing;
            oWord = new Microsoft.Office.Interop.Word.Application();
            oWord = Globals.ThisAddIn.Application;
            foreach (word.Section wordSection in Globals.ThisAddIn.Application.ActiveDocument.Sections)
            {
                        word.HeaderFooter hf = wordSection.Footers[word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
                        hf.LinkToPrevious = false;
                        hf.Range.Font.Size = 7;
                        hf.Range.Font.ColorIndex = word.WdColorIndex.wdBlack;
                        hf.Range.Text = "Inserting Footer Text in current section of a document";
            }
        }

现在我想在当前部分添加页脚,而不是在任何其他部分。

例如,我有一个1,2,3,4,5节的文档,如果我目前在第3节并点击该按钮,我想只为第3节下的页面添加一个脚注。< / p>

1 个答案:

答案 0 :(得分:1)

得到微软博客的答复:

仅为当前部分下的页面添加页脚,首先需要转到下一部分并将LinkToPrevious设置为false,然后返回当前部分以设置页脚。

这是简单的代码。

        Word._Application oWord;
        object oMissing = Type.Missing;
        oWord = Globals.ThisAddIn.Application;
        int sectionIndex = oWord.Selection.Information[Word.WdInformation.wdActiveEndSectionNumber];
        Word.Section wordSection;
        Word.HeaderFooter hf;

        if (sectionIndex != oWord.ActiveDocument.Sections.Count) { 
        wordSection = oWord.ActiveDocument.Sections[sectionIndex+1];
        hf = wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
        hf.LinkToPrevious = false;
        }

        wordSection = oWord.ActiveDocument.Sections[sectionIndex];
        hf = wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
        hf.LinkToPrevious = false;
        hf.Range.Font.Size = 7;
        hf.Range.Font.ColorIndex = Word.WdColorIndex.wdBlack;
        hf.Range.Text = "Inserting Footer Text in current section of a document";