How to capture a search string

C

Carlos Chalhoub

Hi listmates,

I need some help with the following. I am trying to search for the following
string:
 

I cannot have the string repeat itself more than 6 times in a row. I created
a macro that searches and replaces 7 occurences with 6, 8 with 6, etc.
Example:

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ">        </span>"
.Replacement.Text = ">      </span>"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

The problem is that some documents have 16 occurences of this string, and I
don't want to keep adding to the routine above. It's becoming unmanageable.
Maybe somebody has an idea on a way to cut any extraneous strings over the
6-occurrences. I hope I made myself clear.

Best regards
Carlos
 
J

Jonathan West

Hi Carlos,

Simply replace 7 with 6, and repeat the process as many times as necessary
until no more characters have been deleted. Something like this should do
nicely

Dim numChars as Long
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ">       </span>"
.Replacement.Text = ">      </span>"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
End With
Do

numchars = ActiveDocument.Range.End
Selection.Find.Execute Replace:=wdReplaceAll
Loop Until ActiveDocument.range.End = numChars
 
C

Carlos Chalhoub

Thanks for the answer Jonathan. The only problem I see with this is that the
  string is preceded by a > and followed by a </span> tag. This is
important because I do not want to delete this string indiscriminately. Is
it possible to run this script while taking these things into account?
Thanks.

Carlos
 
J

Jonathan West

Carlos Chalhoub said:
Thanks for the answer Jonathan. The only problem I see with this is that the
  string is preceded by a > and followed by a </span> tag. This is
important because I do not want to delete this string indiscriminately. Is
it possible to run this script while taking these things into account?
Thanks.

The code I provided has those elements in the search and replacement
strings, ans so should work fine. Test it out on a copy of a document.
 
C

Carlos Chalhoub

Hi Jonathan,

I did run it. The code only works on instances where the string appears 7
consecutive times (preceded by > and followed by the span tag). It does not
work if the string appears more than 7 consecutive times. Obviously, I can
repeat the search with 8, 9, 10, but that brings me back to the same
predicament, I need one piece of code for all eventualities.

Best regards
Carlos
 
J

Jonathan West

Carlos Chalhoub said:
Hi Jonathan,

I did run it. The code only works on instances where the string appears 7
consecutive times (preceded by > and followed by the span tag). It does not
work if the string appears more than 7 consecutive times. Obviously, I can
repeat the search with 8, 9, 10, but that brings me back to the same
predicament, I need one piece of code for all eventualities.

Ah. Right. Remove the opening > sign from both the find & replace strings.
All should then be well.
 
K

Klaus Linke

Another way to "skin the cat" would have been one wildcard replacement
Find what: (      )( ){1,}
Replace with: \1

(The simpler "Find what: (( ){6})( ){1,}" doesn't work for some
reason or another... Word often doesn't seem to like nested brackets)

Regards,
Klaus
 

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