+ Reply to Thread
Results 1 to 15 of 15

Close the headers and footer to back into the body of the document?

  1. #1
    Registered User
    Join Date
    06-25-2015
    Location
    Toronto
    MS-Off Ver
    2013
    Posts
    43

    Close the headers and footer to back into the body of the document?

    Hi everyone,

    Is there programmatic way to get out of the header footer mode ?

    Thanks,

  2. #2
    Forum Expert
    Join Date
    03-20-2015
    Location
    Primarily UK, sometimes NL
    MS-Off Ver
    Work: Office 365 / Home: Office 2010
    Posts
    2,405

    Re: Close the headers and footer to back into the body of the document?

    A quick 'record macro' for exiting header/footer mode gives me this:
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    Regards,
    Aardigspook

    I recently started a new job so am a bit busy and may not reply quickly. Sorry - it's not personal - I will reply eventually.
    If your problem is solved, please go to 'Thread Tools' above your first post and 'Mark this Thread as Solved'.
    If you use commas as your decimal separator (1,23 instead of 1.23) then please replace commas with semi-colons in your formulae.
    You don't need to give me rep if I helped, but a thank-you is nice.

  3. #3
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Close the headers and footer to back into the body of the document?

    Do note there's probably nothing you'd need to do programmatically that requires switching to the header/footer view to begin with, so not doing so also obviates the need to exit it.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  4. #4
    Registered User
    Join Date
    06-25-2015
    Location
    Toronto
    MS-Off Ver
    2013
    Posts
    43

    Re: Close the headers and footer to back into the body of the document?

    Hi everyone,

    My program needs to go through both Word::Fields and Word::Headers of Word::Document.

    After I call Word::HeaderFooter::get_Range(...), I believe Word automatically goes into Header/Footer mode, and the number of pages are changed too.
    I need to find a way to go back to original mode.

    Word::Window::put_SeekView(...) may not be the right way, but it helps me resolving my problem for now
    ... ...
    Word::WindowPtr pWnd = spDoc->GetActiveWindow();
    Word::ViewPtr pView = pWnd->GetView();
    HRESULT hr = pView->put_SeekView(Word::wdSeekPrimaryHeader);
    hr = pView->put_SeekView(Word::wdSeekMainDocument);
    ... ...

    Thanks,
    Last edited by neu_stone; 10-13-2015 at 10:25 AM.

  5. #5
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Close the headers and footer to back into the body of the document?

    Accessing a header or footer doesn't have any affect of the number of pages in a document. You say you need to access both fields and headers & footers. That can be done without ever switching to the header/footer view. For example, the following code processes fields in the body of the document and in all headers & footers:
    Please Login or Register  to view this content.
    Last edited by macropod; 10-13-2015 at 10:53 AM.

  6. #6
    Registered User
    Join Date
    06-25-2015
    Location
    Toronto
    MS-Off Ver
    2013
    Posts
    43

    Re: Close the headers and footer to back into the body of the document?

    Hi Macropod,

    I did do in the same way.
    My 1-page doc has narrow margins, and there is one table occupying the whole page.

    My codes is like:
    ... ...
    Word::_DocumentPtr spDoc = nullptr;
    hr = pApp->get_ActiveDocument(&spDoc);

    ...
    Word::SectionPtr spSection = nullptr;
    hr = spSections->Item(lIndex, &spSection);
    ASSERT(spSection);

    Word::HeadersFootersPtr spHeaders = nullptr;
    hr = spSection->get_Headers(&spHeaders);
    ASSERT(spHeaders);

    Word::HeaderFooterPtr spPrimaryHeader = nullptr;
    hr = spHeaders->Item(Word::wdHeaderFooterPrimary, &spPrimaryHeader);
    ASSERT(spPrimaryHeader);

    Word::RangePtr spRange = nullptr;
    hr = spPrimaryHeader->get_Range(&spRange);
    ... ...

    Then the status of my doc was changed.


    Xin

    before.png

    after.png

  7. #7
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Close the headers and footer to back into the body of the document?

    When you access the header/footer area for the first time, Word automatically creates an empty paragraph for both the header and the footer. And, because the header & footer now have content, Word allocates whatever the minimum space is that you've provided for the header and footer as part of the page layout. That explains why your content, which is too large for the space allocated to the header and footer, has moved.

  8. #8
    Registered User
    Join Date
    06-25-2015
    Location
    Toronto
    MS-Off Ver
    2013
    Posts
    43

    Re: Close the headers and footer to back into the body of the document?

    Hi Macropod,

    Is there a way to check the empty of Header/Footer, or release the empty paragraph ?

    For now, I have to call Word::View::put_SeekView(...) twice, with wdSeekPrimaryHeader or wdSeekMainDocument respectively. This helps me releasing empty paragraph.

    Thanks,

  9. #9
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Close the headers and footer to back into the body of the document?

    Perhaps you should ask why you're accessing it if you're not putting any content there? The attachment to your previous post indicates it was still empty.

  10. #10
    Registered User
    Join Date
    06-25-2015
    Location
    Toronto
    MS-Off Ver
    2013
    Posts
    43

    Re: Close the headers and footer to back into the body of the document?

    Hi Macropod,

    Let's assume that my program is to perform automation test.
    When test doc is opened, both header/Footer and main body will be automatically gone through for specific data.

    We don't know if there is any content there in Header/Footer.

    Regards,

  11. #11
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Close the headers and footer to back into the body of the document?

    In that case you could test the content using code like:
    Please Login or Register  to view this content.
    Such code won't affect anything.

  12. #12
    Registered User
    Join Date
    06-25-2015
    Location
    Toronto
    MS-Off Ver
    2013
    Posts
    43

    Re: Close the headers and footer to back into the body of the document?

    Hi Macropod,

    Your method works.
    I made small changes to your codes, because .StoryRanges(wdPrimaryHeaderStory) will throw an error in my case.

    Below is my codes in C++:
    ...
    HRESULT hr;
    ///. storyRanges :
    ///. A collection of Range objects that represent stories in a document.
    Word::StoryRangesPtr pStoryRanges = nullptr;
    hr = spDoc->get_StoryRanges(&pStoryRanges);
    ASSERT(pStoryRanges);

    ///. Use StoryRanges(Index), where Index is a WdStoryType constant,
    ///. to return a single story as a Range object.
    Word::RangePtr pPrimaryHeaderRange = nullptr;
    hr = pStoryRanges->raw_Item(Word::wdPrimaryHeaderStory, &pPrimaryHeaderRange);
    if(pPrimaryHeaderRange)
    {
    ...
    }

    ... ...


    Thanks,

  13. #13
    Registered User
    Join Date
    06-25-2015
    Location
    Toronto
    MS-Off Ver
    2013
    Posts
    43

    Re: Close the headers and footer to back into the body of the document?

    Hi Macropot,

    After knowing the count of StoryRanges, for example 5, how to get Range object by index number (not the specified story type) ?

    Thanks,

  14. #14
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Close the headers and footer to back into the body of the document?

    There is no such thing as a count of story ranges. Word has 17 predefined story ranges:
    WdStoryType Enumeration
    Value Name
    1 wdMainTextStory
    2 wdFootnotesStory
    3 wdEndnotesStory
    4 wdCommentsStory
    5 wdTextFrameStory
    6 wdEvenPagesHeaderStory
    7 wdPrimaryHeaderStory
    8 wdEvenPagesFooterStory
    9 wdPrimaryFooterStory
    10 wdFirstPageHeaderStory
    11 wdFirstPageFooterStory
    12 wdFootnoteSeparatorStory
    13 wdFootnoteContinuationSeparatorStory
    14 wdFootnoteContinuationNoticeStory
    15 wdEndnoteSeparatorStory
    16 wdEndnoteContinuationSeparatorStory
    17 wdEndnoteContinuationNoticeStory

    If you want to get the nth field in the Primary Header Story, you would have to reference the nth field in that story. For example:
    Activedocument.StoryRanges(wdPrimaryHeaderStory).Fields(5)
    to reference the 5th field in the Primary Header Story. This gets complicated when your document has multiple Sections with headers that may or may not be linked to those of the preceding Section(s).

  15. #15
    Registered User
    Join Date
    06-25-2015
    Location
    Toronto
    MS-Off Ver
    2013
    Posts
    43

    Re: Close the headers and footer to back into the body of the document?

    Hi Macropod,

    Thanks for your help.

    Regards,

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 0
    Last Post: 09-19-2014, 04:22 PM
  2. Help with Programming of Macro for Batch Header/Footer/Body Text Change
    By Chrisdudley7 in forum Word Formatting & General
    Replies: 7
    Last Post: 06-13-2014, 07:37 PM
  3. Headers and Footer on the Chart do not work??????
    By maperalia in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 03-20-2006, 01:10 PM
  4. Adding Headers and Footer to an Entire Workbook??
    By Spyder in forum Excel General
    Replies: 1
    Last Post: 02-17-2006, 11:30 AM
  5. How to sort in Excel with headers link to all body parts
    By Sorting in Excel for Item with label. in forum Excel General
    Replies: 0
    Last Post: 12-30-2005, 04:25 PM
  6. [SOLVED] Can the find/replace function be used for worksheet headers/footer
    By wz7y2k in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 03-21-2005, 02:06 AM
  7. How do I make the custom headers and footer repeat on every works.
    By Chrisod in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 01-24-2005, 06:07 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1