Repeat for each section

J

Julia

I would like to have the following code repeat for all footers in all
sections, but I can't seem to get it right. Can you help? Thank you!

Dim i As Integer
For i = 1 To ActiveDocument.Sections.Count
With ActiveDocument.Sections(i)
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:= _
"FILENAME \p ", PreserveFormatting:=True
End With
Next i

I'm trying to insert this field in the footer for all sections - the
existing sections may or may not have text or page numbers in them already.
Thanks for your help!
 
J

Jezebel

The problem with your current code is that you are adding the fields to the
Selection, without changing the selection for each section. A better way to
iterate the footers in a document is along these lines --

Dim pFooter as Word.Range

set pFooter = ActiveDocument.StoryRanges(wdPrimaryFooterStory)
Do
pFooter.Fields.Add Range:=pFooter, ....

Set pFooter = pFooter.NextStoryRange
Loop until pFooter is nothing

If you have different odd/even/first page footers, you'll need to put the
whole thing inside another loop --

For pIndex = 1 to 3
Set pFooter = ActiveDocument.StoryRanges(Choose(pIndex, _
wdPrimaryFooterStory, _
wdEvenPagesFooterStory,
_
wdFirstPageFooterStory))
Do ...
Loop
Next
 

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