Text inside footer in all sections

S

Sammy

Hello,

I need to have the filename (on every page) and the page number (on every
page except the first) in the footer, in every section of my document. I
scrub all footers first and I can get that to work. So far I have:

Dim i As Integer
For i = 1 To ActiveDocument.Sections.Count
With ActiveDocument.Sections(i)
.Footers(wdHeaderFooterFirstPage).Range.Delete
.Footers(wdHeaderFooterFirstPage).Range.Fields.Add
_Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"FILENAME \* Lower ", PreserveFormatting:=True

.Footers(wdHeaderFooterPrimary).Range.Delete
.Footers(wdHeaderFooterFirstPage).Range.Fields.Add _
Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"FILENAME \* Lower ", PreserveFormatting:=True

Selection.Sections(1).Footers(1).PageNumbers.Add PageNumberAlignment:= _
wdAlignPageNumberCenter, FirstPage:=False


End With
Next i

IF YOU THINK THIS DOESN'T WORK YOU ARE CORRECT. IN HELP, THEY ALWAYS GIVE
EXAMPLES USING ONLY A CERTAIN MEMBER OF A COLLECTION SO I COULDN'T FIGURE OUT
HOW TO PUT PAGE NUMBERING IN ALL SECTIONS.

CAN YOU HELP? THANKS A LOT
 
S

Stefan Blom

The easiest solution would seem to set up a template for this. But if you
want to do it in VBA, the following variation on your code works:

Sub test()
Dim i As Integer
For i = 1 To ActiveDocument.Sections.Count
With ActiveDocument.Sections(i)
.Footers(wdHeaderFooterFirstPage).Range.Delete
ActiveDocument.Fields.Add _
Range:=.Footers(wdHeaderFooterFirstPage).Range, _
Type:=wdFieldEmpty, Text:= _
"FILENAME \* Lower ", PreserveFormatting:=True

.Footers(wdHeaderFooterPrimary).Range.Delete
ActiveDocument.Fields.Add _
Range:=.Footers(wdHeaderFooterFirstPage).Range, _
Type:=wdFieldEmpty, Text:= _
"FILENAME \* Lower ", PreserveFormatting:=True

.Footers(wdHeaderFooterPrimary).PageNumbers.Add _
PageNumberAlignment:=wdAlignPageNumberCenter, FirstPage:=False


End With
Next i
End Sub

Note that when you add a field, you can use any Fields collection (I used
ActiveDocument.Fields just to make the line a bit shorter), but the Range
parameter of the Add method must carefully reference the Range object to
which you are adding the field. (In particular, you cannot use
Selection.Range.)

--
Stefan Blom
Microsoft Word MVP


in message
news:[email protected]...
 
S

Sammy

Thanks for your help! Okay, I tried it, but it's omitting the filename from
all pages in the section except for the 1st page. In your code it looks like
the filename should be appearing on every page, but it's not. Thanks
 
S

Sammy

One other question if you don't mind, what is the best approach to formatting
the filename field with an 8pt font? I would select and then format after
the field was inserted. Is this the best method? thanks again
 
D

Doug Robbins - Word MVP

Use this corrected, augmented version of Stefan's code:

Dim i As Integer
For i = 1 To 1 'ActiveDocument.Sections.Count
With ActiveDocument.Sections(1)
.Footers(wdHeaderFooterFirstPage).Range.Delete
ActiveDocument.Fields.Add _
Range:=.Footers(wdHeaderFooterFirstPage).Range, _
Type:=wdFieldEmpty, Text:= _
"FILENAME \* Lower ", PreserveFormatting:=True
.Footers(wdHeaderFooterFirstPage).Range.Font.Size = 8
.Footers(wdHeaderFooterPrimary).Range.Delete
ActiveDocument.Fields.Add _
Range:=.Footers(wdHeaderFooterPrimary).Range, _
Type:=wdFieldEmpty, Text:= _
"FILENAME \* Lower ", PreserveFormatting:=True
.Footers(wdHeaderFooterPrimary).PageNumbers.Add _
PageNumberAlignment:=wdAlignPageNumberCenter, firstPage:=False
.Footers(wdHeaderFooterPrimary).Range.Paragraphs(2).Range.Font.Size =
8
End With
Next i

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
S

Stefan Blom

Sorry, it seems as if I didn't read your initial post carefully enough... My
suggestion only inserts the file name in the first page header. The good news
is that Doug has given you what you asked for. :)
 

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