Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Find/Replace one word in all footers

Status
Not open for further replies.

Jtorres13

Technical User
Nov 11, 2006
162
US
Hello. I've read may of the threads about finding/replacing text in footers and have found bits and pieces of what I want to do, but can't ut it all together. Would you please help?

I want to find a word in all fotoers of the document (first page footer, Footer, Even Footer, Odd Footer, etc.). I want to replace that word with a word found in the name of the document...

What I'm trying to do is update the document number, which appears on the document name, in the title bar of the document.

The word contains fixed characters, then a number, then a version number, for example, Doc: #12345v1.

So, when we create a new version, now we need to update the footer. First problem I have is that I"m deleting the footer, so all text goes away, including the page number. This is the code I have for clearing up the footer:
Code:
Sub RemoveHeadAndFoot()
    Dim oSec As Section
    Dim oFoot As HeaderFooter

    For Each oSec In ActiveDocument.Sections
        For Each oFoot In oSec.Footers
            If oFoot.Exists Then oFoot.Range.Delete
        Next oFoot
    Next oSec
End Sub

Second problem, the code puts the new doc number, but only on the current section, not on all possible footers in the document. If the document has multiple sections and those sections are unlinked, the unlinked sections don't get their footers updated. Here's the code we have for that:
Code:
Private Sub insert_footer(strFooterText As String)
Dim footer_selection As Selection
Dim initial_active_window_view_type As Integer
initial_active_window_view_type = ActiveWindow.View.Type
Application.EnableCancelKey = wdCancelDisabled
If (ActiveWindow.View.SplitSpecial = wdPaneNone) Then
ActiveWindow.ActivePane.View.Type = wdPageView
Else
ActiveWindow.View.Type = wdPageView
End If
ActiveWindow.View.SeekView = wdSeekMainDocument
ActiveDocument.ActiveWindow.View.SeekView = wdSeekCurrentPageFooter
Set footer_selection = ActiveWindow.Selection
' Set the footer text to the sole parameter of this function.
footer_selection.Text = strFooterText
ActiveWindow.View.Type = wdPageView
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range = footer_selection.Range
ActiveWindow.View.Type = wdNormalView
ActiveWindow.View.Type = initial_active_window_view_type
Application.EnableCancelKey = wdCancelInterrupt
End Sub

So, what I want to do is to just do a Find/Replace and replace only the doc number.

May I please have some assistance?
 
Try something like the following, which automagically updates the version number for any 4-5 digit number followed by v# where the # is a, say, 1-6 digit number:
Code:
Sub UpdateFooters()
Dim Rng As Range, i As Long
For Each Rng In ActiveDocument.StoryRanges
  Select Case Rng.StoryType
    Case wdEvenPagesFooterStory, _
      wdFirstPageFooterStory, _
      wdPrimaryFooterStory
      With Rng
        If i = 0 Then
          With .Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Format = False
            .Forward = True
            .Wrap = wdFindContinue
            .Text = "([0-9]{4,5}[Vv])[0-9]"
            .Replacement.Text = "\1"
            .MatchWildcards = True
            .Execute
          End With
          i = CLng(Split(UCase(.Duplicate.Text), "V")(1)) + 1
        End If
        With .Find
          .Replacement.Text = "\1" & i
          .Execute Replace:=wdReplaceAll
        End With
      End With
    Case Else
  End Select
Next Rng
End Sub

Cheers
Paul Edstein
[MS MVP - Word]
 
Previous code only seems to work properly on the first Section. Try this instead:
Code:
Sub UpdateFooters()
Dim Scn As Section, HdFt As HeaderFooter, i As Long
For Each Scn In ActiveDocument.Sections
  For Each HdFt In Scn.Footers
    If HdFt.LinkToPrevious = False Then
      With HdFt.Range
        If i = 0 Then
          With .Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Format = False
            .Forward = True
            .Wrap = wdFindContinue
            .Text = "([0-9]{1,4}[Vv])[0-9]"
            .Replacement.Text = "\1" & i
            .MatchWildcards = True
            .Execute Replace:=wdReplaceNone
          End With
          i = CLng(Split(UCase(.Duplicate.Text), "V")(1)) + 1
        End If
        With .Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Format = False
          .Forward = True
          .Wrap = wdFindContinue
          .Text = "([0-9]{1,4}[Vv])[0-9]"
          .Replacement.Text = "\1" & i
          .MatchWildcards = True
          .Execute Replace:=wdReplaceAll
        End With
      End With
    End If
  Next HdFt
Next Scn
End Sub

Cheers
Paul Edstein
[MS MVP - Word]
 
>So, when we create a new version, now we need to update the footer

Possibly too late for current documents, but you might be better off using a field for this value, such as a DocVariable
 
Or, if the filename is the same as the revision name, a simple FILENAME field.

Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top