Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Repeating Finds in a Macro 1

Status
Not open for further replies.
Jun 5, 2002
417
AU
Hi,

I have this macro I recorded to do some fiddling with subsequent lines every time it finds "Singles".

I want this to be repeated until the end of the document, which I can't seem to work out after some investigation.

Here is the macro:

Code:
Sub FrOpenSinglesLines()
' FrOpenSinglesLines Macro
'
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "Singles"
        .Replacement.Text = " "
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.HomeKey Unit:=wdLine
    Selection.MoveDown Unit:=wdLine, Count:=2
    Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.TypeBackspace
    Selection.TypeText Text:=" "
    Selection.HomeKey Unit:=wdLine
    Selection.MoveDown Unit:=wdLine, Count:=2
    Selection.TypeBackspace
    Selection.TypeText Text:=" "
    Selection.HomeKey Unit:=wdLine
    Selection.Range.Case = wdNextCase
End Sub

Any suggestions greatly appreciated!

Peter Moran
 
Try:

Code:
Sub FrOpenSinglesLines()
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "Singles"
        .Replacement.Text = " "
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
[s]    Selection.HomeKey Unit:=wdLine
    Selection.MoveDown Unit:=wdLine, Count:=2
    Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.TypeBackspace
    Selection.TypeText Text:=" "
    Selection.HomeKey Unit:=wdLine
    Selection.MoveDown Unit:=wdLine, Count:=2
    Selection.TypeBackspace
    Selection.TypeText Text:=" "
    Selection.HomeKey Unit:=wdLine
    Selection.Range.Case = wdNextCase
[/s]End Sub


---- Andy

There is a great need for a sarcasm font.
 
to do some fiddling with subsequent lines every time it finds "Singles".
And what, exactly, does that 'fiddling' amount to? While it's easy enough to see what the code is doing, that tells us nothing useful about what's being affected in the underlying document.

Cheers
Paul Edstein
[MS MVP - Word]
 
Hi macropod,
Thanks for your query.
This is what the 'fiddling" does:

It takes text downloaded from the web:

Women’s Singles - Quarterfinals - 1h11
S. Stephens (7)
1 4
J. Konta (26)
6 6
Men’s Singles - Quarterfinals - 1h51
K. Nishikori (7)
1 1 3
R. Nadal (2)
6 6 6

for all matches on a day at the French Tennis Open and combines the name line with the score below it.
So the result is:

Women’s Singles - Quarterfinals - 1h11
S. Stephens (7) 1 4
J. Konta (26) 6 6
Men’s Singles - Quarterfinals - 1h51
K. Nishikori (7) 1 1 3
R. Nadal (2) 6 6 6

I want to find the first "Singles" line and then do the fiddling and then repeat for every "Singles" line for the document.

I hope this makes things clearer.

Peter Moran
 
So, really, all you need is a simple wildcard Find/Replace (not even a macro), where:
Find = (Singles*\))^13(*\))^13
Replace = \1^32\2^32

Cheers
Paul Edstein
[MS MVP - Word]
 
Hi Andrzejek,

Thanks for your reply.
I don't understand what you have done by removing the code I need to perform every time I find the word "Singles" in the document.

There may be 50 "Singles" in the document and I want to perform this code every time "Singles" is found until the end of the document.

My code example only processes the first "Singles" found!

Many thanks.

Peter Moran
 
Hi macropod,

Many thanks!

Will have to check out wildcard Find & Replace in detail. Not one of my strengths!

I did not realise it was so powerful.

Will check it out today.

Also have a STAR!

Peter Moran
 
Since you did not specify what you meant by "fiddling", I assumed you wanted to find word "Singles" one-by-one. Since your code did not (really) do that, my example does find the word if you keep running it over and over.


---- Andy

There is a great need for a sarcasm font.
 
Hi Andrzejek

Thanks for your reply.

The fiddling is incidental, Mainly I wanted to know how to repeatedly Find and then run some code until the end of the document.

My example showed one instance of what I wanted repeated until the end of the document.

Peter Moran
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top