Find and Replace Paragraphs - better explanation

J

Joe Sorrenti

I have to loop through numerous documents in a directory and find and
replace paragraphs. I plan on having one open documnet with two paragraphs,
the first paragraph is the one to find, the second is the one to replace.

Next I need to open the other documents and iterate through the paragraphs.
If the paragraph text equals the first paragraph in the original document, I
would like to replace it with the second paragraph in the original document.

Does anybody have an example of searching for one paragraph and replacing
the text of it with the text of another paragraph?
 
H

Helmut Weber

Hi Joe,
that was my solution at first. But then I discarded it,
as you seemed to want to replace even the original paragraph.
Now you are writing,
the first paragraph is the one to find, the second is the one to replace.
The second or all except the original paragraph?
Don't give up!
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
 
E

Ed

Joe: This worked for me when I tested it. I would recommend making a copy
of your documents before using it the first time. (I'm still fairly novice,
so trust gingerly. Like I said, it worked for *me*!) Also, I used a For
Each loop, which Helmut - who is an MVP - does not like. I assume he has a
good reason for that, so this may warrent some correcting. Other than that,
I hope it helps.

Ed

Sub Repl_Text()

' Set up your "Text Replacement" document so that
' para. 1 is the full path to the directory you need to look through
' para. 2 is the text you need to find and replace
' para. 3 is the replacement text

' This code assumes this document is open when the macro is run.
' It can be modified to let you browse and open it.

Dim objD As Document
Dim objP As Paragraph
Dim strDir As String
Dim strBadTxt As String
Dim strGoodTxt As String
Dim strDoc As String

' Where do I search?
strDir = ActiveDocument.Paragraphs(1).Range.Text
' Remove paragraph mark from string
strDir = Left(strDir, Len(strDir) - 1)
' In case there isn't a final \
If Right(strDir, 1) <> "\" Then
strDir = strDir & "\"
End If

' What am I looking for?
strBadTxt = ActiveDocument.Paragraphs(2).Range.Text

' What do I replace it with?
strGoodTxt = ActiveDocument.Paragraphs(3).Range.Text

' Don't need this doc any more
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges

' Look through directory for Word docs
strDoc = Dir$(strDir & "*.doc")

Do Until LenB(strDoc) = 0

' Set next doc to open to found doc
Documents.Open FileName:=strDir & strDoc
Set objD = ActiveDocument

' Search each paragraph
For Each objP In objD.Paragraphs
If objP.Range.Text = strBadTxt Then
objP.Range.Text = strGoodTxt
End If
Next objP

' Save, close doc
objD.Save
objD.Close

' Next doc
strDoc = Dir$()

Loop

' Done
Set objD = Nothing
MsgBox "All done!"

End Sub
 
J

Joe Sorrenti

Helmut,

Your code works great. Is there any way to also copy the formatting of the
new paragraph into the old one. Currently, I am only able to replace the
text and not formatting.

Joe
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Joe Sorrenti > écrivait :
In this message, < Joe Sorrenti > wrote:

|| Helmut,
||
|| Your code works great. Is there any way to also copy the formatting of
the
|| new paragraph into the old one. Currently, I am only able to replace the
|| text and not formatting.
||

Try this modified version of Helmut's code.
'_______________________________________
Sub Test654v2()

Dim oDcm As Document
Dim sPrg As String
Dim sPrgRge As Range
Dim iPrg As Integer

Set oDcm = ActiveDocument
With oDcm
sPrg = .Paragraphs(1).Range.Text
Set sPrgRge = .Paragraphs(2).Range.FormattedText

For iPrg = 3 To .Paragraphs.Count
With .Paragraphs(iPrg)
If .Range.Text = sPrg Then
.Range.FormattedText = sPrgRge
End If
End With
Next
End With

End Sub
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top