PDA

View Full Version : Solved: Reset all header / footers in document.



WorkerBee
05-23-2005, 11:22 AM
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:
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 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

MOS MASTER
05-23-2005, 11:39 AM
Hi Curt, :yes

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! :whistle:

TonyJollans
05-24-2005, 12:55 AM
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.

sandam
05-24-2005, 01:25 AM
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.

MOS MASTER
05-24-2005, 09:22 AM
Hi Andy, :D

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..:whistle:

WorkerBee
05-24-2005, 03:48 PM
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: '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
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.

MOS MASTER
05-25-2005, 10:53 AM
Hi, :yes

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? :whistle:

WorkerBee
05-25-2005, 11:41 AM
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.

MOS MASTER
05-25-2005, 11:56 AM
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:
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


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! :whistle: