Consulting

Results 1 to 3 of 3

Thread: Solved: Need Help Updating Section 1 Footers

  1. #1
    VBAX Mentor clhare's Avatar
    Joined
    Mar 2005
    Posts
    470
    Location

    Solved: Need Help Updating Section 1 Footers

    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?

    [vba]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[/vba]

  2. #2
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    "When I run this macro, nothing replaces. "

    Nothing replaces because, as Tony points out, you are using Selection (which is NOT in the Footer).[vba]
    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
    [/vba]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •