View Single Post
 
Old 03-19-2013, 02:58 PM
netchie netchie is offline Windows XP Office 2007
Novice
 
Join Date: Jun 2011
Posts: 24
netchie is on a distinguished road
Default Find/Replace Macro Problem

Found this while surfing and uses it to replace words(s). The problem is if the word searched by macro is correct, it still corrected the word plus moving the last letter.

Example: From "Replace" will be added "d" and changed it to "Replaced". Some are corrected but some that searched as "Replaced" will changed to "Replacedd" (adding another d).

How can I fix this?
Code:
Sub Replaced()
Dim myStoryRange As Range
'First search the main document using the Selection
With Selection.Find
  .Text = "Replace"
  .Replacement.Text = "Replaced"
  .Forward = True
  .Wrap = wdFindContinue
  .Format = False
  .MatchCase = False
  .MatchWholeWord = False
  .MatchWildcards = False
  .MatchSoundsLike = False
  .MatchAllWordForms = False
  .Execute Replace:=wdReplaceAll
End With
'Now search all other stories using Ranges
For Each myStoryRange In ActiveDocument.StoryRanges
  If myStoryRange.StoryType <> wdMainTextStory Then
    With myStoryRange.Find
      .Text = "Replace"
      .Replacement.Text = "Replaced"
      .Wrap = wdFindContinue
      .Execute Replace:=wdReplaceAll
    End With
    Do While Not (myStoryRange.NextStoryRange Is Nothing)
      Set myStoryRange = myStoryRange.NextStoryRange
      With myStoryRange.Find
        .Text = "De"
        .Replacement.Text = "Replaced"
        .Wrap = wdFindContinue
        .Execute Replace:=wdReplaceAll
      End With
    Loop
  End If
Next myStoryRange
End Sub

Last edited by macropod; 03-19-2013 at 03:07 PM. Reason: Added code tags & formatting
Reply With Quote