PDA

View Full Version : Solved: Need Help Updating Section 1 Footers



clhare
01-27-2010, 12:10 PM
I have to replace the default footer text in alot of templates and I'm having trouble getting it to work. The first part of the default text is always the same, so I want to search for that, then extend the line (except for the return) and replace it. When I run this macro, nothing replaces. What am I doing wrong?

Sub ReplaceFooterText()
Dim intHFType As Integer
Dim rngPane As Range
With ActiveDocument.Sections(1)
For intHFType = 1 To 3
Set rngPane = .Footers(intHFType).Range
With rngPane.Find
' Search for default text and replace it
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "987654321 A54321 112233abc"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
End With
Selection.Find.Execute
' If found, update the text
If Selection.Find.Found = True Then
' Extend text to include the file number text
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
' Move left to exclude the return
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
' Insert new text
Selection.TypeText Text:="123456 54321 112233abc [kit#] [class#]"
End If
End With
.Footers(intHFType).Range.Borders(wdBorderTop).LineStyle = wdLineStyleNone
Next intHFType
End With
End Sub

TonyJollans
01-28-2010, 12:05 AM
Although you have a "With rngPane.Find" block, you are using Selection.Find inside the block, so your search is of the Selection, not the Footer text.

fumei
01-28-2010, 09:55 AM
"When I run this macro, nothing replaces. "

Nothing replaces because, as Tony points out, you are using Selection (which is NOT in the Footer).
Sub ReplaceFooterText()
Dim intHFType As Integer
Dim rngPane As Range
With ActiveDocument.Sections(1)
For intHFType = 1 To 3
Set rngPane = .Footers(intHFType).Range
With rngPane.Find
' Search for default text and replace it
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(FindText:="987654321 A54321 112233abc", _
Forward:=True) = True
With rngPane
.Text = ""
.Expand Unit:=wdParagraph
.MoveEnd Unit:=wdCharacter, Count:=-1
' Insert new text
.InsertAfter ("123456 54321 112233abc [kit#] [class#]")
.Collapse 0
End With
Loop
End With
.Footers(intHFType).Range.Borders(wdBorderTop) _
.LineStyle = wdLineStyleNone
Next intHFType
End With
End Sub