none
How to change Word document footer correctly? RRS feed

  • Question

  • According to PIA object model Word document is divided to some structure named "Sections". Also "Document" class contain property Sections for accessing to document sections.

    I want to edit page header and footer and add some text. I have try to change header and footer it using Sections property as in sample:

    foreach (Section section in _document.Sections)
                {
                    if (section.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range != null)
                    {
                        section.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.InsertAfter("Some text");
                    }
                }
    

     


    So, above sample is easy: I just loop over ech section in document and change section footer.

    But for some documents different sections contain one footer, and above sample add text to footer few times. 

    So, it looks like section refers to footer from another section and accessing to footer from different sections return footer that is general for both sections.

    Is my above code sample is correct?

    Does it possible change footer one time (above sample invoke footer changing few times)?

     



    • Edited by digor_ua Wednesday, October 19, 2011 10:32 AM
    Wednesday, October 19, 2011 10:28 AM

Answers

  • Sorry, I have found a solution

    This is not a word PIA issue, this is a bug in our application. I have to fix it.

    Sorry for incovience and close the topic

    Thursday, October 20, 2011 9:55 AM

All replies

  • Hi Digor

    I'm not sure exactly what you mean by "But for some documents different sections contain one footer, and above sample add text to footer few times"

    Do you mean you see Some textSome textSome text (the text is repeated many times?

    Range.InsertAfter will not replace what's already in a footer, it will add to what's already there. Could that explain what you're seeing?

    If you want to replace the current content with new content, then use Range.Text = "Some text";


    Cindy Meister, VSTO/Word MVP
    Wednesday, October 19, 2011 2:07 PM
    Moderator
  • Sorry, I have found a solution

    This is not a word PIA issue, this is a bug in our application. I have to fix it.

    Sorry for incovience and close the topic

    Thursday, October 20, 2011 9:55 AM
  • Hi Digor

    Thank you for letting us know :-)


    Cindy Meister, VSTO/Word MVP
    Thursday, October 20, 2011 9:57 AM
    Moderator