AutoOpen Footer Updates

T

Tywardreath

Hi

I have a template that is set up with headers/footers, different first page.
I use the following macro to update the filename field within the footer/s.

Sub AutoOpen()
Dim oField As Word.Field
Dim oRange As Word.Range
Set oRange = ActiveDocument.Sections(1).Footers(2).Range

If oRange.Fields.Count >= 1 Then
For Each oField In oRange.Fields
If oField.Type = wdFieldFileName Then
oField.Update
End If
Next
End If
End Sub

My problem is that it only updates the filename on the first page. Is there
anyway to change the code so it updates it on the 2nd and subsequent pages?

Cheers, Ty
 
T

Tywardreath

I've been trying to use something with:
ActiveDocument.StoryRanges(wdPrimaryFooterStory).Fields.Update
ActiveDocument.StoryRanges(wdFirstPageFooterStory).Fields.Update

but I get unstuck if the document that is being opened doesn't have footers
different first page. I'm quite perplexed now :(

I'd like to use the same code for AutoOpen and AutoNew.

Cheers, Ty
 
T

Tywardreath

I've found the following code, which seems to work. The only problem is that
it doesn't work when Word is initialy opened. Any ideas?


Sub AutoNew()
Dim Story As Variant
Dim rngNext As Word.Range

' Iterate through all story types
For Each Story In ActiveDocument.StoryRanges

' Only update fields in a footer
If Story.StoryType = wdPrimaryFooterStory Or _
Story.StoryType = wdFirstPageFooterStory Or _
Story.StoryType = wdEvenPagesFooterStory Then

' Update fields in this footer
Story.Fields.Update

' There may be linked footers so update them as well
Set rngNext = Story.NextStoryRange
Do Until rngNext Is Nothing

' Update fields in this footer
rngNext.Fields.Update

' Link to next story (if any)
Set rngNext = rngNext.NextStoryRange
Loop
End If
Next
End Sub
 
C

Chuck Henrich

The following code is a bit simpler. It goes through every footer in every
section, and updates the fields.

Dim ftrFooter As HeaderFooter
Dim x As Long

With ActiveDocument

For x = 1 To .Sections.Count
For Each ftrFooter In .Sections(x).Footers
ftrFooter.Range.Fields.Update
Next ftrFooter
Next x

End With

I've tested it in an AutoNew macro and it seems to work fine.
 

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