Automating Word - First Page Headers Appearing After Rotating a Page To Landscape

Posted on 2011-10-06
Last Modified: 2012-05-12
Using VB.Net 2010 (Pro) to create Word documents (either Word 2003 or 2010)

This is an offshoot from a previous question that can be found HERE.

I am creating a document that has a header/footer the appears on the first page, and a different header/footer that is to appear on all the other pages.  This has been working fine until now.

In the previous question noted above, I needed to be able to rotate a single page in the middle of the document to landscape.  I did, and that part works fine.  However there appears to be an artifact that was caused by the rotation.  On the page that rotated, as well as the following page (in my testing that following page is the last page of the document), the header and footer are the ones that are to appear on the first page.

Here's the code that "rotates" the page:
m_oDoc.Bookmarks.Item("\endofdoc").Range.PageSetup.Orientation = wdOrientLandscape

Open in new window

After this, comes the code that writes to the page
Then the code to return it back to portrait:
m_oDoc.Bookmarks.Item("\endofdoc").Range.PageSetup.Orientation = wdOrientPortrait

Open in new window

How do I get the correct header/footer to show up?

Question by:Clif
    LVL 83

    Expert Comment

    LVL 76

    Accepted Solution

    Each section can have its own first page setting. By default, the settings for new sections are copied from the previous one, so in your case, when you create a new section, you will need to reset the different first page setting for the new sections

    m_oDoc.Sections.Last.PageSetup.DifferentFirstPageHeaderFooter = False
    LVL 11

    Author Closing Comment

    That was exactly what I needed.  Thanks.

    I'm sure your link would have lead to a solution, but time is of the essence, so a code snippet wins out over a link.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now