Results 1 to 4 of 4
  1. #1
    2 Star Lounger
    Join Date
    Jul 2003
    Location
    Park City, Utah
    Posts
    189
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Search and Replace All (once again) (2003)

    I have the following code in a macro which repeats for 22 different search and replace text strings. (It was set up this way to allow for simple construction of templates where the user just puts text between square brackets and the system will replace with a variable derived from a text file. The template creator just has to put the text [variable text name] in the document for it to be filled in.)

    With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "[Subject Name]"
    .Replacement.Text = strSubjectName
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
    End With

    and, of course, it does not replace text in headers or footers. I tried adding the following, but it did not work. What am I doing wrong? Technically it oonly needs to search and replace in headers and footers. There will never be any footnotes, endnotes, section breaks, etc. in these documents.

    Dim rngMyStory As Range

    For Each rngMyStory In ActiveDocument.StoryRanges
    With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "[Subject Name]"
    .Replacement.Text = strSubjectName
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
    End With
    Next rngMyStory

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Search and Replace All (once again) (2003)

    See Replace Text Anywhere in a Document on the Word MVP site.

  3. #3
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Search and Replace All (once again) (2003)

    Try
    With myStoryRange.Find

    in place of
    With Selection.Find

    Alan

  4. #4
    2 Star Lounger
    Join Date
    Jul 2003
    Location
    Park City, Utah
    Posts
    189
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Search and Replace All (once again) (2003)

    Thank you, Alan, that was it, of course.

Posting Permissions

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