Word VBA >> Find and Replace Paragraphs

by Joe Sorrenti » Tue, 27 Jul 2004 22:24:21 GMT



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.






Word VBA >> Find and Replace Paragraphs

by Helmut Weber » Tue, 27 Jul 2004 23:20:38 GMT


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




Similar Threads

1. Macro to find and replace paragraph markers with tabs

Hi

I'm trying to convert several tables in Word 2003 to standard tabbed text so 
I can import it into Excel.

I've recorded a macro that converts the table to text seperated by tabs but 
then I need to replace the remainder paragraphs with tabs. Recording the 
macro goes OK.

The table gets converted and the text is still selected so Find, Replace ^p 
with ^t and I get asked to check all the doc after it's replaced the 
selection, I say No and close the dialog box. When I run the macro it 
converts ALL paras to tabs and not just the selection. This is the macro I've 
recorded. Am I missing something??

Many thanks

Ian

Sub Macrotable()
'
' 
' Macro recorded 01/06/2009 by me
'
    Selection.HomeKey Unit:=wdLine
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Selection.ConvertToTable Separator:=wdSeparateByDefaultListSeparator, _
        NumColumns:=3, NumRows:=1, AutoFitBehavior:=wdAutoFitFixed
    With Selection.Tables(1)
        .Style = "Table Grid"
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = True
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = True
    End With
    Selection.MoveDown Unit:=wdLine, Count:=2
    Selection.Tables(1).Select
    Selection.Rows.ConvertToText Separator:=wdSeparateByTabs, NestedTables:= _
        True
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "^p"
        .Replacement.Text = "^t"
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    
    Selection.Find.Execute Replace:=wdReplaceAll
    End With
    Selection.MoveDown Unit:=wdLine, Count:=1
End Sub

2. Find and Replace Paragraphs - MS Word&VBA

3. Find and Replace Paragraphs - better explanation

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?



4. How to Find a 'Printing' Paragraph symbol using 'Find & Replace' & wildcard - Word Document management

5. How to Find a 'Printing' Paragraph symbol using 'Find & Replace'

A 'printing' paragraph symbol cannot be pasted in the Find or Replace 
fields. Does anyone know the code for a 'printing' paragraph symbol that can 
work in the Find and Replace fields?

Regards,

AN 


6. find & replace but don't replace one character in find

7. BUG REPORT - Paragraph mark in Find & Replace

There is a bug in Office Word 2007 12.0.4518.1014.

Description: "^v" is inserted instead of "^p" into the Find Word field of 
Find & Replace screen when Pragraph Character is selected from the Special 
pull-down button.

Occurs: Always

Steps to reproduce:
1. Open Find & Replace window (eg press Control+H)
2. Select either Find or Replace Tab
3. Press More >> button if required, to see the bottom panel
4. Ensure all tickboxes are un-ticked
5. Focus on the Find What field
6. Click on the Special pull-down button and select Paragraph Character from 
the list.

Not sure where to submit this bug to. Someone please confirm this bug, so 
Microsoft techies take notice.

8. "Paragraph character" in find and replace - MS Word Support