Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #3  
Old 03-19-2013, 02:58 PM
netchie netchie is offline Word VBA - Search a word and add/remove it Windows XP Word VBA - Search a word and add/remove it Office 2007
Novice
Word VBA - Search a word and add/remove it
 
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
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
search on multiple word documents Guy Roth Word 7 03-06-2017 01:31 PM
Word VBA - Search a word and add/remove it Word: Search and fill from Excel gbrucken Word 1 04-30-2012 10:40 AM
Word VBA - Search a word and add/remove it Remove numbering from Word Doc antaeusguy Word 1 03-14-2012 04:01 AM
Word VBA - Search a word and add/remove it search option in word 2007 anano Word 1 01-11-2012 09:03 AM
Search Problems in MS Word LoganB Word 0 09-03-2010 06:49 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:09 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft