Search and Replace Header Text

User avatar
Sundog
5StarLounger
Posts: 704
Joined: 28 Jan 2010, 22:47
Location: Alien Country (Roswell NM)

Search and Replace Header Text

Post by Sundog »

In Word 2007, my document has many sections (due to changes from Portrait to Landscape and back). I need to change ALL header text for ALL sections. It seems that Search & Replace only works on the current header, and I must Next Section over and over. Any other way?
Sundog

User avatar
HansV
Administrator
Posts: 78523
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SEarch and Replace Header Text

Post by HansV »

You have to loop somehow, there is no "simple" solution - see for example Replace Text Anywhere in a Document on the Word MVP site.
Best wishes,
Hans

User avatar
Sundog
5StarLounger
Posts: 704
Joined: 28 Jan 2010, 22:47
Location: Alien Country (Roswell NM)

Re: Search and Replace Header Text

Post by Sundog »

Tried the reference, didn't work, probably due to incorrect line breaks?

Code: Select all

Public Sub FindReplaceAnywhere()

  Dim rngStory As Word.Range

  Dim pFindTxt As String

  Dim pReplaceTxt As String

  Dim lngJunk As Long

  Dim oShp As Shape

  pFindTxt = InputBox("Enter the text that you want to find.", "C//FGI-MOD")

  If pFindTxt = "" Then

    MsgBox "Cancelled by User"

    Exit Sub

  End If

TryAgain:

  pReplaceTxt = InputBox("Enter the replacement.", "C/FGI-MOD")

  If pReplaceTxt = "" Then

    If MsgBox("Do you just want to delete the found text?", vbYesNoCancel) = vbNo Then

      GoTo TryAgain

    ElseIf vbCancel Then

      MsgBox "Cancelled by User."

      Exit Sub

    End If

  End If

  'Fix the skipped blank Header/Footer problem

  lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType

  'Iterate through all story types in the current document

  For Each rngStory In ActiveDocument.StoryRanges

    'Iterate through all linked stories

    Do

      SearchAndReplaceInStory rngStory, pFindTxt, pReplaceTxt

      On Error Resume Next

      Select Case rngStory.StoryType

      Case 6, 7, 8, 9, 10, 11

        If rngStory.ShapeRange.Count > 0 Then
        For Each oShp In rngStory.ShapeRange
        
            If oShp.TextFrame.HasText Then
            SearchAndReplaceInStory oShp.TextFrame.TextRange, pFindTxt, pReplaceTxt

            End If

          Next

        End If

      Case Else

        'Do Nothing

      End Select

      On Error GoTo 0

      'Get next linked story (if any)

      Set rngStory = rngStory.NextStoryRange

    Loop Until rngStory Is Nothing

  Next

End Sub

Public Sub SearchAndReplaceInStory(ByVal rngStory As Word.Range, ByVal strSearch As String, ByVal strReplace As String)

  With rngStory.Find

    .ClearFormatting

    .Replacement.ClearFormatting

    .Text = strSearch

    .Replacement.Text = strReplace

    .Wrap = wdFindContinue

    .Execute Replace:=wdReplaceAll

  End With

End Sub

Sundog

User avatar
HansV
Administrator
Posts: 78523
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Search and Replace Header Text

Post by HansV »

Although the code you posted contains lots of superfluous line breaks, that doesn't influence its operation. I copied/pasted the code from your post into a module and tested it on a document with several sections, text boxes and shapes with text. It worked flawlessly. What exactly was the problem that you encountered?
Best wishes,
Hans

User avatar
Sundog
5StarLounger
Posts: 704
Joined: 28 Jan 2010, 22:47
Location: Alien Country (Roswell NM)

Re: Search and Replace Header Text

Post by Sundog »

Many header text remained unchanged.
Sundog

User avatar
HansV
Administrator
Posts: 78523
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Search and Replace Header Text

Post by HansV »

Could you post a small sample document - with dummy text if necessary - in which header text doesn't get replaced correctly? Please indicate what text you searched for and what you wanted to replace it with.
Best wishes,
Hans

User avatar
Sundog
5StarLounger
Posts: 704
Joined: 28 Jan 2010, 22:47
Location: Alien Country (Roswell NM)

Re: Search and Replace Header Text

Post by Sundog »

Gave it a rest overnight. :cloud9: :snore:

Tried same code again, but this time let it run for a long time (44 Sections) :time: , and this time it worked.

Maybe I inadvertently stopped execution the first time. :woops:

Thanks for the link! :wink:
Sundog

rhelmer
NewLounger
Posts: 3
Joined: 06 Jan 2011, 18:15

Re: Search and Replace Header Text

Post by rhelmer »

So where do you put the code, that works, in what module?

User avatar
StuartR
Administrator
Posts: 12612
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Search and Replace Header Text

Post by StuartR »

rhelmer wrote:So where do you put the code, that works, in what module?
That code can go in any ordinary module.
StuartR


rhelmer
NewLounger
Posts: 3
Joined: 06 Jan 2011, 18:15

Re: Search and Replace Header Text

Post by rhelmer »

Do you mean macro?

User avatar
StuartR
Administrator
Posts: 12612
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Search and Replace Header Text

Post by StuartR »

rhelmer wrote:Do you mean macro?
Yes.

Alt-F11 - to bring up the Visual Basic Editor
Insert > Module - to create an empty module where you can insert macros
Copy the code and paste it into the module
Click RUN
StuartR


rhelmer
NewLounger
Posts: 3
Joined: 06 Jan 2011, 18:15

Re: Search and Replace Header Text

Post by rhelmer »

Thank you. It worked!

User avatar
Sundog
5StarLounger
Posts: 704
Joined: 28 Jan 2010, 22:47
Location: Alien Country (Roswell NM)

Re: Search and Replace Header Text

Post by Sundog »

To work with all Word documents, I believe the proper place is in a new Module under the Normal listing. Word gurus, please correct me if I'm wrong.

[Word Ribbon => Developer => Visual Basic => Normal => Modules => (top menu) Insert => Module.]
Sundog

User avatar
HansV
Administrator
Posts: 78523
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Search and Replace Header Text

Post by HansV »

Yes - macros that should be available in all documents can be stored in a module in the Normal template. Macros that should be available only to documents based on a specific template should be stored in a module in that template, and macros that should be available in one document only should go into a module in that document.
Best wishes,
Hans

User avatar
Sundog
5StarLounger
Posts: 704
Joined: 28 Jan 2010, 22:47
Location: Alien Country (Roswell NM)

Re: Search and Replace Header Text

Post by Sundog »

Thanks for the additional info, Hans.
Sundog