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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I make my loop stop at the bottom of the file? 1

Status
Not open for further replies.

MaryAnn1985

Technical User
Oct 6, 2008
6
FI
I am writing a Microsoft Word macro and I want the macro to search the whole file for specific word endings and to write something in front of the words that have these specific endings. I used a loop, because the macro has to search the whole file for these endings, but it has to search the file only once (not over and over again), so I want the macro to stop at the bottom of the file. How can I do this? Thanks in advance!

This is an early version of the macro (including only one ending, namely -aisia ) :

Do
Selection.Find.ClearFormatting
With Selection.Find
.Text = "aisia "
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
If Not Selection.Find.Found Then Exit Do
Selection.MoveLeft Unit:=wdWord, Count:=1
Selection.TypeText Text:="<partitive>"
Loop Until Not Selection.Find.Found
End Sub
 
Hi MaryAnn,

1.) your macro would not do anything, because the ".execute" is missing.

2.) Even with ".execute" it won't work properly, as you move left one word once you've found an instance. If you now restart the search, it will find the same word over and over again and keep typing your tag before it.

3.) set your Find options outside the loop, that's enough. Then you only have to ".execute" within the loop, the options will remain just as set before.

4.) Execute the Find once before the loop and check at loop entry rather then loop end. This will make your if condition unnecessary.

This is an (untested!) improved version of your macro, which should do just fine, and be quite a bit faster:
Code:
Selection.HomeKey unit:=wdStory
With Selection.Find
    .Text = "aisia "
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    [b].Execute
End With
Do While Selection.Find.Found
    Selection.Range.InsertBefore "<partitive>"
    Selection.Collapse wdCollapseEnd
    Selection.Find.Execute
Loop[/b]
Good luck!
;-)

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
Hi MakeItSo,

Thanks very much :)!!! It works fine now (without finding the same ending over and over again)! The only thing that does not work properly, it that the tag is inserted in front of the ending and not in front of the whole word. I will try to fix it..

 
Hi MaryAnn,

Try:
Code:
With ActiveDocument.Content.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Text = "aisia "
  .Replacement.Text = "^&<partitive> "
  .Forward = True
  .Wrap = wdFindContinue
  .Format = False
  .MatchCase = False
  .MatchWholeWord = False
  .MatchWildcards = False
  .MatchSoundsLike = False
  .MatchAllWordForms = False
  .Execute Replace:=wdReplaceAll
End With
Note that the above code adds a space after '<partitive>', but you can delete that if it's not what you want.

Cheers

[MS MVP - Word]
 
Hi Maryann,

sorry, I should have realised as much. [blush]

Simply replace this line
Selection.Range.InsertBefore "<partitive>"

with this one:
Selection.Range.Words(1).InsertBefore "<partitive>"

That should already do it.
;-)

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
As an FYI for anyone interested, there was another answer given at anther thread (was originally posted in the wrong forum):
thread68-1504915


--

"If to err is human, then I must be some kind of human!" -Me
 
Thanks everyone!! I used this macro as the basis of my actual macro that searches for about 50 different word endings and it does fine!
MaryAnn
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top