Find and Replace Paragraphs

J

Joe Sorrenti

I have a requirement to loop through all Word documents in a directory and
if they contain a certain paragraph, replace it with a new paragraph.

Selection.Paragraphs(1).Range returns the text I am looking for. I need to
take this and replace paragrahs in a document. Any help appreciated.
 
H

Helmut Weber

Hi Joe,
I am assuming that looping through all docs in a directory
(if they are there, then you are lucky), is not a problem.
Have a look at this:
Sub Test654()
Dim oDcm As Document
Dim sPrg As String ' the text in the paragraph the cursor is in
Dim iPrg As Integer ' a paragraph counter
Set oDcm = ActiveDocument
sPrg = Selection.Paragraphs(1).Range.Text
For iPrg = 1 To oDcm.Paragraphs.Count
If oDcm.Paragraphs(iPrg).Range.Text = sPrg Then
' next line is for testing only
oDcm.Paragraphs(iPrg).Range.Select
MsgBox "replace me"
End If
Next
End Sub
There is another approach using "for each ... next",
which don't like too much for reasons, that are not relevant here.
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
 
Top