Insert field in footer - all sections

J

Julia

Hi, thanks for your help.

I have the following macro:

If Documents.Count = 0 Then Exit Sub

Dim i As Integer
For i = 1 To ActiveDocument.Sections.Count
With ActiveDocument.Sections(i)
.Footers(wdHeaderFooterFirstPage).Range.Delete
.Footers(wdHeaderFooterPrimary).Range.Delete
End With
Next i

This works great to delete all footers, but now I'm wanting to insert a date
field in all footers (all sections), and can't seem to use the same formula,
substituting the .range.delete of course. What am I doing wrong?? Thanks
again!
 
J

Jean-Guy Marcil

Julia was telling us:
Julia nous racontait que :
Hi, thanks for your help.

I have the following macro:

If Documents.Count = 0 Then Exit Sub

Dim i As Integer
For i = 1 To ActiveDocument.Sections.Count
With ActiveDocument.Sections(i)
.Footers(wdHeaderFooterFirstPage).Range.Delete
.Footers(wdHeaderFooterPrimary).Range.Delete
End With
Next i

This works great to delete all footers, but now I'm wanting to insert
a date field in all footers (all sections), and can't seem to use the
same formula, substituting the .range.delete of course. What am I
doing wrong?? Thanks again!

Here is one way of doing it: Link up all footers, then inert the date only
in the first setion. This way no need to delte anything and you insert the
date only at one place, easier to handle later if you want to odify its
format.

'_______________________________________
Dim i As Integer

With ActiveDocument.Sections
For i = 1 To .Count
.Item(i).Footers(wdHeaderFooterFirstPage).LinkToPrevious = True
.Item(i).Footers(wdHeaderFooterPrimary).LinkToPrevious = True
Next i
.Item(1).Footers(wdHeaderFooterFirstPage).Range.InsertDateTime _
DateTimeFormat:="yyyy-MM-dd", InsertAsField:=True
.Item(1).Footers(wdHeaderFooterPrimary).Range.InsertDateTime _
DateTimeFormat:="yyyy-MM-dd", InsertAsField:=True
End With
'_______________________________________


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Julia

Thanks for your response. Sorry, I should have mentioned, there is existing
text and or page numbers in the multiple sections, so I'm having to go into
each footer in each section and place the date field in the upper left-hand
corner of the footer. Do you have an example of how I can do this with my
existing code??

Thanks again
 
J

Jean-Guy Marcil

Julia was telling us:
Julia nous racontait que :
Thanks for your response. Sorry, I should have mentioned, there is
existing text and or page numbers in the multiple sections, so I'm
having to go into each footer in each section and place the date
field in the upper left-hand corner of the footer. Do you have an
example of how I can do this with my existing code??

Your original post stated:

"This works great to delete all footers, but now I'm wanting to insert a
date
field in all footers (all sections), "

My crystal ball being currently at the repair shop, I could not tell with my
habitual sagacity that you actually meant that you did not mean to delete
the footer content. :p
The way I read it I thought you wanted to delete all footers and replace
them with the date.

This can be quite messy depending on the actual footer content.

If it is simple one-line paragraphs all formatted with the same style, then
it can be easy. If you have disparate content, it can be messy.

Please elaborate on the footer content, style and text disposition.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
C

Chuck

You need to work with ranges (collapsing, moving starts and ends, etc) to
position the insertion point:

Sub InsertFieldInFooter()

Dim i As Long
Dim ftr As HeaderFooter
Dim sec As Section
Dim rng As Range

With ActiveDocument
For i = 1 To .Sections.Count
'loop through each section in the
'document

'you don't need to address each
'header specifically, you can loop
'through all three headers (FirstPage,
'Primary, OddEven) automatically:
For Each ftr In .Sections(i).Footers
'set your range for wherever you
'want your date field to be, this
'assumes the end of the footer:
Set rng = ftr.Range
rng.Collapse wdCollapseEnd
.Fields.Add _
Range:=rng, _
Type:=wdFieldDate, _
Text:="\@ ""dd MMMM yyyy""", _
PreserveFormatting:=True
Next ftr
Next i
End With

End Sub

HTH
Chuck
 
J

Jean-Guy Marcil

Chuck was telling us:
Chuck nous racontait que :
You need to work with ranges (collapsing, moving starts and ends,
etc) to position the insertion point:

Sub InsertFieldInFooter()

Dim i As Long
Dim ftr As HeaderFooter
Dim sec As Section
Dim rng As Range

With ActiveDocument
For i = 1 To .Sections.Count
'loop through each section in the
'document

'you don't need to address each
'header specifically, you can loop
'through all three headers (FirstPage,
'Primary, OddEven) automatically:
For Each ftr In .Sections(i).Footers
'set your range for wherever you
'want your date field to be, this
'assumes the end of the footer:
Set rng = ftr.Range
rng.Collapse wdCollapseEnd
.Fields.Add _
Range:=rng, _
Type:=wdFieldDate, _
Text:="\@ ""dd MMMM yyyy""", _
PreserveFormatting:=True
Next ftr
Next i
End With

End Sub

HTH
Chuck

This will add the date at the end of the footer, next to whatever last
character is in this footer. She wants the date in the upper left hand
corner...
Can be tricky, depending on actual footer content.
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
C

Chuck

The code I posted was for illustration/teaching purposes not as a plug and
play solution. Obviously users have to adapt any posted code to their own
needs -- that's why I pointed out that she would need to work with ranges to
position the insertion point. If a user wants to put the date field at the
beginning of the footer, she should simply replace "wdCollapseEnd" with
"wdCollapseStart" in the code I posted. If the field goes elsewhere in the
footer, it's up to the user to manipulate the range to get the insertion
point where she wants it. Just because something is tricky doesn't mean it
can't be done -- or taught or learned.
 

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