find and replace (simple VBA)
-
Hello all -- i have a problem that is really getting me down . . . I am selecting a line in MS Word and trying to remove all double spaces " " and replace them with single spaces -- until I only have single spaces. i.e. " hello world" => " hello world" I created a _very_ short macro that loops until the selection returns a 'false' value. Do Loop Until Selection.Find.Execute(Replace:=wdReplaceOne, ReplaceWith:=" ", FindText:=" ") After debugging for a while I found that what is happening is that the selection is changing each time -- I tried the following to correct this: Dim blnEnd As Boolean With Selection Do blnEnd = .Find.Execute(FindText:=" ", ReplaceWith:=" ", Replace:=wdReplaceOne) Loop While blnEnd = True End With but my selection keeps changing just like before -- how do I iterate through a selection that does not change? please help -- this is a killin' me :confused: tim --------------------------------------- Tim Booher
-
Hello all -- i have a problem that is really getting me down . . . I am selecting a line in MS Word and trying to remove all double spaces " " and replace them with single spaces -- until I only have single spaces. i.e. " hello world" => " hello world" I created a _very_ short macro that loops until the selection returns a 'false' value. Do Loop Until Selection.Find.Execute(Replace:=wdReplaceOne, ReplaceWith:=" ", FindText:=" ") After debugging for a while I found that what is happening is that the selection is changing each time -- I tried the following to correct this: Dim blnEnd As Boolean With Selection Do blnEnd = .Find.Execute(FindText:=" ", ReplaceWith:=" ", Replace:=wdReplaceOne) Loop While blnEnd = True End With but my selection keeps changing just like before -- how do I iterate through a selection that does not change? please help -- this is a killin' me :confused: tim --------------------------------------- Tim Booher
Here's a script that works:
Sub Macro8() Dim fContinue As Boolean fContinue = True Dim Sel As Range Do While fContinue Set Sel = Selection.Range Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find .Text = " " .Replacement.Text = " " .Forward = True .Wrap = wdFindAsk .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With fContinue = Selection.Find.Execute(Replace:=wdReplaceOne) Selection.Start = Sel.Start Selection.End = Sel.End Loop End Sub
Volodya Orlenko, orlenko [at] hotmail [dot] com