Turning Off Same as Previous

J

jerem

Just a heads up, VBA knowledge: low novice

Aside from manually going into each Header or Footer and deselecting Same as
Previous (the bane of Word's existence) does anyone out there have a macro to
do it in one sweep (taking out every designation in all Headers and Footers).
Had one in my previous job, but did not think to take it with me.

Thanks in advance.
 
G

Greg Maxey

Sub ScratchMacro()
Dim oSect As Section
Dim oFooter As HeaderFooter
Dim oHeader As HeaderFooter
Dim i As Long
For Each oSect In ActiveDocument.Range.Sections
For i = 1 To 3
Set oFooter = oSect.Footers(Choose(i, wdHeaderFooterFirstPage, _
wdHeaderFooterPrimary, wdHeaderFooterEvenPages))
Set oHeader = oSect.Headers(Choose(i, 1, _
wdHeaderFooterPrimary, wdHeaderFooterEvenPages))
Set oHeader = oSect.Headers(Choose(i, wdHeaderFooterFirstPage, _
wdHeaderFooterPrimary, wdHeaderFooterEvenPages))
oFooter.LinkToPrevious = False
oHeader.LinkToPrevious = False
Next
Next oSect
End Sub
 
G

Greg Maxey

I suppose it doesn't have to be as lengthy as my previous post:

Sub ScratchMacro()
Dim oSect As Section
Dim i As Long
For Each oSect In ActiveDocument.Range.Sections
For i = 1 To 3
oSect.Footers(i).LinkToPrevious = False
oSect.Headers(i).LinkToPrevious = False
Next
Next oSect
End Sub
 
R

Russ

Greg,
I think this macro changes literally the first three headers and footers of
each section? What if there is less than three or more than three headers
and footers?
 
G

Greg Maxey

Russ,

That code is the same as doing this:

Sub ScratchMacroII()
Dim oSect As Section
For Each oSect In ActiveDocument.Range.Sections
oSect.Footers(wdHeaderFooterPrimary).LinkToPrevious = False
oSect.Footers(wdHeaderFooterEvenPages).LinkToPrevious = False
oSect.Footers(wdHeaderFooterPrimary).LinkToPrevious = False
oSect.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
oSect.Headers(wdHeaderFooterEvenPages).LinkToPrevious = False
oSect.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
Next oSect
End Sub

Look up one of the elements in "( )" using the object browser and you will
see the contstant values of 1 2 or 3.

AFAIK, each there are three storytypes available to each section (no more
and no less) regardless if they are physically used in the document.
 
J

jerem

Turned off Same as Previous on a doc with 10 Section breaks so you're right -
the short version works fine. Thanks.
 
R

Russ

Greg,
So headers and footers collections are indexed differently.
Does this mean that if I wanted to change the .linktoprevious on the the
tenth page of the section only (unusual, I know), I can't use for example
oSect.Footers(10).linktoprevious = false?
I'd have to use something like pseudocode:
oSect.Page(10).footers(1).linktoprevious = false?

Or what would be the best way to it?
 
G

Greg Maxey

Russ,

The code I meant to post is:

Sub ScratchMacroII()
Dim oSect As Section
For Each oSect In ActiveDocument.Range.Sections
oSect.Footers(wdHeaderFooterFirstPage).LinkToPrevious = False
oSect.Footers(wdHeaderFooterPrimary).LinkToPrevious = False
oSect.Footers(wdHeaderFooterEvenPages).LinkToPrevious = False
oSect.Headers(wdHeaderFooterFirstPage).LinkToPrevious = False
oSect.Headers(wdHeaderFooterEvenPages).LinkToPrevious = False
oSect.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
Next
End Sub

There are six story ranges that are associated with Headers and Footers. A
First Page, Primary (Odd Pages), and Even Pages. Every section can contain
all six. AFAIK there is no way that you can isolate a single page (e.g.,
page 10) of a section to toggle the LinkToPrevious attribute.

If you wanted to remove link to previous say in section 4 footer Even pages
only then you could code like this:

ActiveDocument.Sections(4).Footers(wdHeaderFooterEvenPages).LinkToPrevious =
False

or substitute wdHeaderFooterEvenPages with its constant "3"

ActiveDocument.Sections(4).Footers(3).LinkToPrevious = False
 
F

fumei via OfficeKB.com

Actually, every Section does contain all six. There is no "can". They do.
 
R

Russ

Greg and fumei,
OK, thanks for the info.
Russ,

The code I meant to post is:

Sub ScratchMacroII()
Dim oSect As Section
For Each oSect In ActiveDocument.Range.Sections
oSect.Footers(wdHeaderFooterFirstPage).LinkToPrevious = False
oSect.Footers(wdHeaderFooterPrimary).LinkToPrevious = False
oSect.Footers(wdHeaderFooterEvenPages).LinkToPrevious = False
oSect.Headers(wdHeaderFooterFirstPage).LinkToPrevious = False
oSect.Headers(wdHeaderFooterEvenPages).LinkToPrevious = False
oSect.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
Next
End Sub

There are six story ranges that are associated with Headers and Footers. A
First Page, Primary (Odd Pages), and Even Pages. Every section can contain
all six. AFAIK there is no way that you can isolate a single page (e.g.,
page 10) of a section to toggle the LinkToPrevious attribute.

If you wanted to remove link to previous say in section 4 footer Even pages
only then you could code like this:

ActiveDocument.Sections(4).Footers(wdHeaderFooterEvenPages).LinkToPrevious =
False

or substitute wdHeaderFooterEvenPages with its constant "3"

ActiveDocument.Sections(4).Footers(3).LinkToPrevious = False
 
G

Greg Maxey

Yes I know that. I waffled about just stating that, but thought it might
make things confusing when many sections "appear" to contain only one type
of header/footer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top