Loop through find/replace results

M

mcambrose

I have a document with the following text "Verse 1", "Verse 2", "Verse 3",
etc in which I want find every occurence of Verse #(whatever the number is)
and add a paragraph mark after the verse number. I can't figure out how to
get the value (the text string found) of the find command. For example when I
find the first occurence (e.g. verse 1) how to I get this value? If I can get
the value of the find result then I will set it equal to a variable and then
add a return to the end and replace the string. I use the find "verse #" to
do my find since the verse number changes, but I need the result to construct
the replace string. I would also like to change the style of the text I find,
but I think I can figure that out.

Example Text

Verse 3 This is the latest ..
Verse 4 This is the newest
Verse 5 This is it

I want to have the following after find/replace
Verse 3
This is the latest ..
Verse 4
This is the newest
Verse 5
This is it
Thanks
 
H

Helmut Weber

Hi,

you don't need a variable at all.

Sub Testabc()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "(Verse [0-9]{1;})[ ]{1;}"
.MatchWildcards = True
.Replacement.Text = "\1^p"
.Replacement.Font.Bold = True
.Execute Replace:=wdReplaceAll
End With
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
H

Helmut Weber

....

use

.Text = "(Verse [0-9]{1,})[ ]{1,}"

comma instead of semi-colon for most countries.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
M

mcambrose

Thanks to both of you who replied. Both solutions worked. However, for some
reason using the find and replace dialog box (insted of using VBA) put a
leading space after the paragraph mark that was inserted, so the next line
started with a space. The VBA example allowed me to also set the style of the
replacement text which was the next step I was going to try to figure out. I
had never really delved deeply into the find/replace wildcard characters
before, but from the example provided, I can see that these are very powerful
tools. I don't fully understand the \1 usage. Thanks again
 
H

Helmut Weber

Hi mcambrose,

in the find and replace dialog
in the search for textbox you can organize the text
to be searched for in separate expressions.
These expressions are marked by parentheses.
In the replace with textbox the expressions are referenced
with a backslash and a number. 1 for the first etc.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 

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