PDA

View Full Version : [SOLVED:] Search & Replace Second Hyphen - From Paragraphs



dj44
05-25-2017, 09:24 AM
folks,,

good thursday

I have got stuck on how to replace the second hyphen in some paragraphs





Sub Second_Hyphen()

Dim lngIndex As Long: lngIndex = 1

For Each oPara In ActiveDocument.Paragraphs


If InStr(1, oPara.Range.Text, "Test1") > 0 Then

Set oRng = oPara.Range

With oRng.Find

.Text = "-"
.MatchWholeWord = True

.Replacement.Text = "[Delete]"

.Execute Replace:=wdReplaceOne '<< only the second hyphen
End With
End If

Next oPara


End Sub



thank you for any advice on this loop that i couldnt implement

gmaxey
05-25-2017, 01:42 PM
Sub Second_Hyphen()
Dim lngIndex As Long
For Each oPara In ActiveDocument.Paragraphs
lngIndex = 0
If InStr(1, oPara.Range.Text, "Test1") > 0 Then
Set oRng = oPara.Range
With oRng.Find
.Text = "-"
.MatchWholeWord = True
Do While .Execute
If lngIndex = 0 Then
lngIndex = lngIndex + 1
oRng.Collapse wdCollapseEnd
Else
oRng.Text = "[Delete}"
Exit Do
End If
Loop
End With
End If
Next oPara
End Sub

dj44
05-25-2017, 02:26 PM
Hello Greg

hope you doing good

Thank you very much for rescuing this loop, I love your magic -

becuase i was doing something in excel and the formulas weres so long and confusing,
I had to come to word, then i spent a few hours testing paragraphs untill i couldnt make it work.

Well it started off well but then I added some loops actually 2 loops an i loop and a j loop and then it got a bit well off track

Thank you very much Greg I really appreciate your code because I do add lots on to it later but that gets me in a different sort of trouble :grinhalo:

Great evening Greg

and folks too