![]() |
#1
|
|||
|
|||
![]() Hello members, I am using the code below posted below to find and replace within range that I have assigned to selection.range but the problem here is it not only replacing within range but beyond the range. Code:
Sub nnewreplace() Dim e(), f() e = Array(" ", " ", Chr(160), Chr(9), "\(([a-z]{1,})\)") f = Array("", "", "", "", "\1.") Dim i As Integer Dim rng As Range Set rng = Selection.Range For i = LBound(e) To UBound(e) With rng.find .ClearFormatting .MatchWildcards = True .Text = e(i) .Replacement.Text = f(i) .Wrap = wdFindStop End With rng.find.Execute Replace:=wdReplaceAll Next End Sub Last edited by Bikram; 04-22-2022 at 04:24 AM. Reason: Added additional details |
#2
|
|||
|
|||
![]()
Try the code below. By the way, you are searching for spaces twice.
Code:
Sub nnewreplace() Dim e(), f() e = Array(" ", " ", Chr(160), Chr(9), "\(([a-z]{1,})\)") f = Array("", "", "", "", "\1.") Dim i As Integer Dim rng As Range Set rng = Selection.Range For i = LBound(e) To UBound(e) With rng.Find .ClearFormatting .MatchWildcards = True .Text = e(i) .Replacement.Text = f(i) .Wrap = wdFindStop .Execute Replace:=wdReplaceAll End With Next End Sub |
#3
|
|||
|
|||
![]()
Thank you, Peterson, for your suggestion. But the code I posted and the code you replied to seem the same.
|
#4
|
|||
|
|||
![]()
One thing that I am wondering here is why is it replacing all the items within the selection and only the last item is being replaced outside the selection. Isn't there any way to constrain the replacement within the selection or range?
|
#5
|
|||
|
|||
![]()
No, it's not the same.
Your code wasn't working because the Execute method was outside of the With statement. |
#6
|
|||
|
|||
![]()
Thanks!! Peterson.
|
#7
|
||||
|
||||
![]()
There is also the potential that the rng is collapsing after the first pass. In my experience, when you use rng.Find the rng collapses to the found instances so a second pass may not include the rng you thought you were looking for.
To avoid this you could redefine the rng before each pass. Code:
Sub nnewreplace() Dim e(), f() e = Array(" ", " ", Chr(160), Chr(9), "\(([a-z]{1,})\)") f = Array("", "", "", "", "\1.") Dim i As Integer, rng As Range For i = LBound(e) To UBound(e) Set rng = Selection.Range With rng.Find .ClearFormatting .MatchWildcards = True .Text = e(i) .Replacement.Text = f(i) .Wrap = wdFindStop .Execute Replace:=wdReplaceAll End With Next End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#8
|
|||
|
|||
![]()
Thank you very much Andrew, for the reply. I tried to redefine the rng each time as you did but it still replaces the whole document below the selection. The rng collapses mainly on the 4th item after passing .Execute Replace:=wdReplaceAll before this line the rng was the original range. But when the code passes this line on the 4th item I got the rng = "". I couldn't find any explainable reason on the internet so I tried the following code of line temporarily
![]() Code:
If i = 4 Then .Execute Replace:=wdReplaceOne Else .Execute Replace:=wdReplaceAll End If |
#9
|
||||
|
||||
![]()
The code I provided looks like it sticks to the selection so I'm not sure why it is failing for you.
Is the first/last character in your selection one of the found items?
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#10
|
|||
|
|||
![]()
If the found item is the first character of the selection then wdreplaceall replaces beyond the selection if the found item is other than the first character then the code works fine and only replaces within the selection.
|
![]() |
|