c#仅在当前节中添加页脚

c#仅在当前节中添加页脚,c#,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;

我正在使用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节下的页面添加页脚注释

已从Microsoft博客获得回复:

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

下面是简单的代码

        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";

那么,问题是你的循环要通过每个部分,你想知道你在哪个部分,这样你就可以在那里添加页脚了吗?wordSection的索引告诉我我在哪个部分。但是如何检查该部分是否为文档的当前/活动部分。例如,我在100页文档的第12页。第12页在第3节下。当我在第12页时,我点击按钮,我想为第3节的所有页面添加页脚。但是我没有看到wordSection的任何属性,无法知道这是否是活动分区。现在循环正在每个分区中添加页脚。我当前无法使用Microsoft.Office.Interop.Word.\u应用程序,如果没有属性获取当前分区,在添加页脚之前,您是否查看过是否有某种特殊字符或隐藏的部分开头/结尾无法检查。