Excel Hints

Results 1 to 9 of 9

Thread: Solved: Reset all header / footers in document.

  1. #1

    Solved: Reset all header / footers in document.

    Hi!

    I have need to reset ALL headers and footers to NOT "Link to previous," in documents which have a lot of sections.

    I've figured out enough to use the following:
    VB:
    Dim mySection As Section, myHF As HeaderFooter 
     
    For Each mySection In ActiveDocument.Sections() 
        For Each myHF In mySection.Headers 
            myHF.LinkToPrevious = False 
        Next 
        For Each myHF In mySection.Footers 
            myHF.LinkToPrevious = False 
        Next 
    Next 
    
    
    Formatting tags added by mark007
    HOWEVER, this takes quite a while to run, as it's nested loops. Is there a command I'm missing which would universally set all headers and/or footers to not LinkToPrevious? Or perhaps somebody can see a way to make the above code more efficient?

    -Curt

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi Curt,

    No there isn't any generic command to reset ALL headers and Footers in the document to NOT linktoprevious. (With one command)

    The problem is that each section has three different Headers and three different footers. (If they are used or not is not the issue..you wanted all of them)

    Those Headers and footers can be changed in each section so Yes you should go in to every header and loop through all three to make sure you have them all!

    The most common Item is wdHeaderFooterPrimary (Header/Footer)
    If you only want to change those your code could be much faster...

    The code you have now is pretty much what you have to do. Sure you can make variations on that to see if one is faster then the other but still you have to nest loop through all those Headers and Footers...

    Enjoy!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  3. #3
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,290
    Location
    Hi Curt,

    I don't know whether there might be a more efficient way to do this - I would be interested to see if there were performance differences between, say, explicitly referencing each header and footer rather than iterating the collections or perhaps walking the Sections collection backwards, but I suspect not. I don't know for sure how Word handles it but I would guess every time you break a link that a copy of the header (or footer) has to be created and that this is inherently slow if you have many sections.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  4. #4
    Knowledge Base Approver
    Space Cadet
    VBAX Tutor sandam's Avatar
    Joined
    Jan 2005
    Location
    London
    Posts
    292
    Location
    I cananswer this one with a bit of my own experience - the only way to ensure that the link to previous is removed - do it manually. Code works but I've found it can be tempremental. As for performance, its slow, although if before you run the sub you set the screen updating property to false first it may be slightly quicker.
    Nothing annoys a non-conformist more than other non-conformists who refuse to conform to the rules of non-conformity.


    Confused is my normal state of mind


  5. #5
    Administrator
    VP-Knowledge Base VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi Andy,

    Do it manually??
    I've never experienced problems with coding LinktoPrevious!

    I'dd love to see the code that has caused you problems in the past..
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  6. #6

    Funny you should ask...

    Actually, the entire reason I was asking about how to globally break all section links is because I was having trouble getting consistant results with code doing it.

    I have a loop which jumps to each section, selects the header, deletes all text in the header, sets LinkToPrevious as False, then moves on.

    A small percentage of the time, SOME of the links remain set to Same As Previous.

    If I step through the code, everything works perfectly. If I run it, some sections don't get unlinked.
    Here it is:
    VB:
     'Clear Header
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader 
     
    With Selection 
        .WholeStory 
        .HeaderFooter.LinkToPrevious = False 
        .Delete Unit:=wdCharacter, Count:=1 
        .Style = conPageHeader 
    End With 
     
     'Clear Footer
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter 
    With Selection 
        .WholeStory 
        .HeaderFooter.LinkToPrevious = False 
        .Delete Unit:=wdCharacter, Count:=1 
        .Style = conPageFooter 
    End With 
     
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument 
    
    
    Formatting tags added by mark007
    Perhaps you can see something that makes it not work about 1 in 15 times, except when it's being stepped-through.

    So as (yet another) workaround, I'm now going through the entire document and deleting text, then running this different code to unlink all the headers and footers in a separate pass.

  7. #7
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi,

    Using the Selection Object will slow your code down and is a higher risk for errors. (Specif towards the panes)

    Can you first confirm to me that the code you've posted first didn't do the job? (About the linktoprevious bit)

    If so do you have a test document for me that gives you problems so I can try to code it different for you?
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  8. #8
    Thanks for the help-

    Yes, the first-posted code DOES work correctly (the one that starts with

    For Each
    mySection In ActiveDocument.Sections()

    I was using THAT code as a workaround for the other code which was not reliable.

    Would it make more sense for me to use the For Each structure to do the other tasks I want to do in the headers and footers? Can I delete text, set the style, etc with this method?

    As for supplying test material, all the test documents I have are real-thing confidential-content documents which I cannot make public. It's mostly just a lot of text on pages. My code breaks each page into a section, and hard-numbers the page/sections. This creates a document in which pages are inflexibly numbered, but that's what we want.

  9. #9
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Yes you should go with the For Next structure.

    And shure you can do whatever you like to the Header or Footer while you're in the loop.

    No testing material and I can understand why...so briefly tested:
    VB:
    Option Explicit 
    Sub CleanHF() 
        Dim mySection As Section, myHF As HeaderFooter 
         
        For Each mySection In ActiveDocument.Sections() 
            For Each myHF In mySection.Headers 
                With myHF 
                    .LinkToPrevious = False 
                    With .Range 
                        .Delete 
                         '                .Style = conPageHeader 'Test youreself
                    End With 
                End With 
            Next 
            For Each myHF In mySection.Footers 
                With myHF 
                    .LinkToPrevious = False 
                    With .Range 
                        .Delete 
                         '                .Style = conPageFooter 'Test youreself
                    End With 
                End With 
            Next 
        Next 
    End Sub 
    
    
    Formatting tags added by mark007
    Does work wel for me so give it a spin. It removes all from H&F and Unlinks the H&F.....

    O do the test on Style youreself I don't have Styles by these names.

    Enjoy!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

Posting Permissions

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