View Full Version : Trying to loop in Word 2010 find and replace
KyrgyzGnome
01-21-2013, 02:37 PM
I can't figure out why this code isn't working. Please help
If Selection.Find.Text = "^p^p" Then
With Selection.Find
.Text = "^p^p"
.Replacement = "^p"
'I don't think this part works. Why can't I just use the replacement
'function like I would if >it wasn't a conditional?
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Loop
'This is one of the places I think I'm losing it. I looked in some forums
'and saw them use this loop feature at the end of a process. I'm sure this
'is wrong,though.
Else
End If
gmaxey
01-21-2013, 03:25 PM
It doesn't work because it doesn't compile. For one, you have a Loop statement without a correspoding Do statement. What are you trying to do?
With Selection.Find
.Text = "^p^p"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
KyrgyzGnome
01-21-2013, 03:48 PM
Greg,
I want it to loop through the find and replace again and run. For instance, say that there are 4 paragraphs returned one after the other (e.g. ^p^p^p^p). I want it to run through find and replace until there is only one (^p) left. That is why I tried to throw the loop tool in.
gmaxey
01-21-2013, 05:36 PM
Use this instead:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
With Selection.Find
.Text = "^13{2,}"
.Replacement.Text = "^p"
.Forward = True
.MatchWildcards = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
KyrgyzGnome
01-21-2013, 06:05 PM
Greg,
Thanks for the help, but this still doesn't work?
.Text = "^13{2,}"
That doesn't return and results. I'm not super familiar with VBA syntax, but is that supposed to have it search for multiple instances (2 or more) of the term before?
gmaxey
01-22-2013, 06:20 AM
Yes, that is what is does (or should). Try this as I forgot to clear formattting>
Sub Macro1()
With Selection.Find
.ClearFormatting
.Text = "^13{2,}"
.Replacement.ClearFormatting
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
ShowVisualBasicEditor = True
End Sub
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.