none
Add headers and footers for landscape and portrait pages RRS feed

  • Question

  • I have created a word document using C# and I have added headers and footers. Some of the pages are portrait and some are landscape and therefore the header and footer I have added don't suit both page layouts.

    How can I add headers and footers to fit the page orientation.

    In my code for adding the headers I have:

    Range headerRng = oDoc.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;

    I thought maybe I could add a header for every section eg. oDoc.Sections[2], oDoc.Sections[3] etc but this didn't work.

     

    All help appreciated. Thank you!

     

    Thursday, November 17, 2011 9:49 PM

Answers

  • It would be even safer to loop the Sections collection and explicitly set the LinkToPrevious property to false for each type of footer (and/or header). This is a VBA example:
     
    Sub UnlinkAllHeadersFooters()
     
    Dim s As Section
    For Each s In ActiveDocument.Sections
    s.Headers(1).LinkToPrevious = False
    s.Headers(2).LinkToPrevious = False
    s.Headers(3).LinkToPrevious = False
    s.Footers(1).LinkToPrevious = False
    s.Footers(2).LinkToPrevious = False
    s.Footers(3).LinkToPrevious = False
    Next s
     
    End Sub

    Stefan Blom, Microsoft Word MVP
    • Marked as answer by Bruce Song Monday, December 5, 2011 9:51 AM
    Sunday, November 20, 2011 3:35 PM

All replies

  • By default, headers (and footers) are linked to their respective “neighbors” in the preceding sections, which means that you have to unlink them first (set the LinkToPrevious property to FALSE for each header and footer within each section).
     
     

    Stefan Blom, Microsoft Word MVP
    Thursday, November 17, 2011 10:23 PM
  • Hi J Eng,

    This is surely possible. I have written a code a VBA which makes it possible. Just follow the procedure and I am sure you can make it in C# as well

    'Go to the page after which the page orientation is changed ( change form portrait to Landscape)

    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

    'Insert a page break to break orientation

    Selection.InsertBreak Type:=wdSectionBreakNextPage

    'Go to the next page footer and break to "Link to previous" link of the footer.

    Selection.HeaderFooter.LinkToPrevious = Not Selection.HeaderFooter. _
            LinkToPrevious

    'Enter the text you would like to see in the footer of the second page

    Selection.TypeText Text:="Ankur Chakravarty"

    Simple as that. Try to follow the steps in C# and you can achieve your goal. Let me know if you have more queries.

     

     

     

     

     


    Ankur Chakravarty Hyderabad
    Sunday, November 20, 2011 2:43 PM
  • It would be even safer to loop the Sections collection and explicitly set the LinkToPrevious property to false for each type of footer (and/or header). This is a VBA example:
     
    Sub UnlinkAllHeadersFooters()
     
    Dim s As Section
    For Each s In ActiveDocument.Sections
    s.Headers(1).LinkToPrevious = False
    s.Headers(2).LinkToPrevious = False
    s.Headers(3).LinkToPrevious = False
    s.Footers(1).LinkToPrevious = False
    s.Footers(2).LinkToPrevious = False
    s.Footers(3).LinkToPrevious = False
    Next s
     
    End Sub

    Stefan Blom, Microsoft Word MVP
    • Marked as answer by Bruce Song Monday, December 5, 2011 9:51 AM
    Sunday, November 20, 2011 3:35 PM