WORD 2003 Complex Replace Problem

G

Guest

I have a complicated Find and Replace problem in WORD 2003 under WIN XP.

I have a word doocement that contains strings similar to the following:
*11(1) [5] [85] [97] [104] [149]; *29(2) [226]

I want to find all entries *11(1) followed by a string of references each
one enclosed in square brackets and the whole "element" terminated by a
semi-colon. Having found them, I want to replace those references which are
less than [145]. So in the example above, I want to end up with:
*11(1) [149]; *29(2) [226]

However if I wanted to remove all references less than, say, [150] then I
would want the result to be:
*29(2) [226]
i.e removing the complete entry including the 11(1) becasue it is now
redundant.

Is it feasible to do this using Find and Replace or must I resign myself to
doing it manually?

Many thanks in advance to anyone who can help.
 
G

Graham Mayor

You can't do this with replace in a single pass. You may be able to do it
with a sequence of
replace functions, but much depends on how many numbers there are in the
[149] segment. The following macro will work with all numbers from 100 to
199. If you have numbers higher or lower than that then you'll have to
modify or add to the search and replace strings.

The macro is essentially four sequences of replace
100-139, 140-144, 145-149 then 150 to 199
The first two are replaced with nothing, the second two with the contents of
the first two bracketed sections - \1\2

The search pattern - (\*[0-9]{2}\([0-9]\))[
0-9\[\]]{21}(\[14[0-4]\]; )(http://www.gmayor.com/replace_using_wildcards.htm
) assumes that there will always be the same number of digits in the
blocks - *11(1) [5] [85] [97] [104] [149]; - though what the digits are in
all but the last block doesn't matter.

Sub ReplaceList()
Dim vFindText As Variant
Dim vReplText As Variant
Dim i As Long

vFindText = Array("(\*[0-9]{2}\([0-9]\))[ 0-9\[\]]{21}(\[1[0-3][0-9]\]; )",
_
"(\*[0-9]{2}\([0-9]\))[ 0-9\[\]]{21}(\[14[0-4]\]; )", _
"(\*[0-9]{2}\([0-9]\))[ 0-9\[\]]{21}(\[14[5-9]\]; )", _
"(\*[0-9]{2}\([0-9]\))[ 0-9\[\]]{21}(\[1[5-9][0-9]\]; )")

vReplText = Array("", "", "\1\2", "\1\2")

With Selection.Find
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
.Format = True
.MatchCase = True

For i = LBound(vFindText) To UBound(vFindText)
.Text = vFindText(i)
.Replacement.Text = vReplText(i)
.Execute replace:=wdReplaceAll
Next i
End With

End Sub

See http://www.gmayor.com/installing_macro.htm


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

You can't do this with replace in a single pass. You may be able to do it
with a sequence of
replace functions, but much depends on how many numbers there are in the
[149] segment. The following macro will work with all numbers from 100 to
199. If you have numbers higher or lower than that then you'll have to
modify or add to the search and replace strings.

The macro is essentially four sequences of replace
100-139, 140-144, 145-149 then 150 to 199
The first two are replaced with nothing, the second two with the contents of
the first two bracketed sections - \1\2

The search pattern - (\*[0-9]{2}\([0-9]\))[
0-9\[\]]{21}(\[14[0-4]\]; )(http://www.gmayor.com/replace_using_wildcards.htm
) assumes that there will always be the same number of digits in the
blocks - *11(1) [5] [85] [97] [104] [149]; - though what the digits are in
all but the last block doesn't matter.

Sub ReplaceList()
Dim vFindText As Variant
Dim vReplText As Variant
Dim i As Long

vFindText = Array("(\*[0-9]{2}\([0-9]\))[ 0-9\[\]]{21}(\[1[0-3][0-9]\]; )",
_
"(\*[0-9]{2}\([0-9]\))[ 0-9\[\]]{21}(\[14[0-4]\]; )", _
"(\*[0-9]{2}\([0-9]\))[ 0-9\[\]]{21}(\[14[5-9]\]; )", _
"(\*[0-9]{2}\([0-9]\))[ 0-9\[\]]{21}(\[1[5-9][0-9]\]; )")

vReplText = Array("", "", "\1\2", "\1\2")

With Selection.Find
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
.Format = True
.MatchCase = True

For i = LBound(vFindText) To UBound(vFindText)
.Text = vFindText(i)
.Replacement.Text = vReplText(i)
.Execute replace:=wdReplaceAll
Next i
End With

End Sub

See http://www.gmayor.com/installing_macro.htm


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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