Help making macro repeat

S

Steve Lowe

Hi,

I've put together the following code, but how do I make it repeat
until it reaches the end of the document


Regards

Splowe.





Sub ParFindandFormat()


Selection.HomeKey Unit:=wdStory



Selection.Find.ClearFormatting
With Selection.Find
.Text = "Paragraph"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute

If Selection.Find.Found = True Then

'Make this line bold and underline

Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Font.Bold = wdToggle
If Selection.Font.Underline = wdUnderlineNone Then
Selection.Font.Underline = wdUnderlineSingle
Else
Selection.Font.Underline = wdUnderlineNone
End If

'Go to end of line

Selection.EndKey Unit:=wdLine

'Insert a blank line

Selection.TypeParagraph

End If

End Sub
- Steve Lowe
- E-Mail : (e-mail address removed)
- Before Replying Remove .NO.SPAM
- UK Resident although my e-mail address is usa.net
 
C

Charles Kenyon

Why not simply use replace with formatting? Not sure that you need a macro
but you could record one. Go to the replace dialog (Ctrl-H) and click on the
More button to give you access to format.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide


--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
D

Doug Robbins - Word MVP

The following is the syntax that you use to get a find repeat until it
reaches the end of the document

Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="MacWordNew", MatchWildcards:=False,
Wrap:=wdFindContinue, Forward:=True) = True
Selection.Paragraphs(1).Range.Delete
Loop
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
S

Steve Lowe

Hi,

Thanks -

I understand the do while .Execute(FindText .......) = True Loop

Not sure why the Selection.Paragraphs(1).Range.Delete
line is there as it deletes the text I'm looking for

If I remove this line the macro get's stuck in a endless loop

In between the do while and the loop command I have the following
lines

'Go to end of line
Selection.EndKey Unit:=wdLine
'Insert a blank line
Selection.TypeParagraph

I would have expected this to insert a new line and then the find
command to find the text occurance of the target text.





The following is the syntax that you use to get a find repeat until it
reaches the end of the document

Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="MacWordNew", MatchWildcards:=False,
Wrap:=wdFindContinue, Forward:=True) = True
Selection.Paragraphs(1).Range.Delete
Loop
End With

- Steve Lowe
- E-Mail : (e-mail address removed)
- Before Replying Remove .NO.SPAM
- UK Resident although my e-mail address is usa.net
 
D

Doug Robbins - Word MVP

That was just an example to give you the syntax. To do what you actually
want to do, use:

Dim myrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="fox", MatchWildcards:=False, _
Wrap:=wdFindStop, Forward:=True) = True
Set myrange = Selection.Range
myrange.End = myrange.Bookmarks("\line").Range.End
myrange.Font.Bold = True
myrange.Font.Underline = wdUnderlineSingle
Selection.Collapse wdCollapseEnd
Loop
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
S

Steve Lowe

Thanks Doug - have got there by a slightly different route but your
example was very helpful.

Regards

splowe.


That was just an example to give you the syntax. To do what you actually
want to do, use:

Dim myrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="fox", MatchWildcards:=False, _
Wrap:=wdFindStop, Forward:=True) = True
Set myrange = Selection.Range
myrange.End = myrange.Bookmarks("\line").Range.End
myrange.Font.Bold = True
myrange.Font.Underline = wdUnderlineSingle
Selection.Collapse wdCollapseEnd
Loop
End With

- Steve Lowe
- E-Mail : (e-mail address removed)
- Before Replying Remove .NO.SPAM
- UK Resident although my e-mail address is usa.net
 

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