none
Detect if the section I am in is the current section RRS feed

  • Question

  • I am using c# and Microsoft.Office.Interop.Word;.

    Here is the for loop I am using to go through every section of a document and adding a footer when I click on a button.

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

    Now I would like to add the footer only in current section and not in any other section. 

    So for example, I have a document with section 1,2,3,4,5 and if I am currently at section 3 and click on that button, I want to add a footernote only for pages under section 3. 

    • Moved by Fei Hu Tuesday, April 10, 2018 3:12 AM Moved Fron C#
    Monday, April 9, 2018 7:31 PM

Answers

  • Hello newbiesaspnet,

    You could use Range.Information to get the current selected section index.

    For adding footernote only for pages under current section, you need first go to next section and set LinkToPrevious as false and then back to current section for setting footernote.

    Here is the simply code.

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

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by newbiesaspnet Wednesday, April 11, 2018 6:41 PM
    Tuesday, April 10, 2018 3:14 AM

All replies

  • https://social.msdn.microsoft.com/Forums/office/en-US/home?category=officedev

    Maybe, the office development forum can help you.

    Monday, April 9, 2018 9:35 PM
  • I am using c# and Microsoft.Office.Interop.Word;.

    Here is the for loop I am using to go through every section of a document and adding a footer when I click on a button.

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

    Now I would like to add the footer only in current section and not in any other section. 

    So for example, I have a document with section 1,2,3,4,5 and if I am currently at section 3 and click on that button, I want to add a footernote only for pages under section 3. 

    Tuesday, April 10, 2018 2:58 AM
  • Hi newbiesaspnet,

    According to your question is more related to office , I will move the thread to Word for Developers forum  for better support.

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 10, 2018 3:10 AM
  • Hello newbiesaspnet,

    You could use Range.Information to get the current selected section index.

    For adding footernote only for pages under current section, you need first go to next section and set LinkToPrevious as false and then back to current section for setting footernote.

    Here is the simply code.

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

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by newbiesaspnet Wednesday, April 11, 2018 6:41 PM
    Tuesday, April 10, 2018 3:14 AM