3

I have recorded a simple macro to find the word "Quantity", go to the end of that line and insert a carriage return. I need to repeat it to the end of the document and quit, or else I'll have an infinite loop.

The code:

     Selection.Find.ClearFormatting
     With Selection.Find
    .Text = "Quantity:"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = True
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.EndKey Unit:=wdLine
Selection.TypeParagraph  
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user3689278
  • 31
  • 1
  • 1
  • 2
  • And what is the issue you are having? As with all programming, please be specific. – indivisible May 29 '14 at 22:10
  • The word "quantity" repeats an unspecified number of times in the document, and I don't know how to repeat the macro until the end of the document and stop. – user3689278 May 29 '14 at 22:16

2 Answers2

2

Change you code to this, note the use of wdFindStop.

Selection.Find.ClearFormatting
     With Selection.Find
    .Text = "Quantity:"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = True
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With

do while Selection.Find.Execute = true
   Selection.EndKey Unit:=wdLine
    Selection.TypeParagraph  
loop

If you have the type of documents this can fail on you can use the Selection.Start by replacing the loop like this:

Dim lastPos As Long
lastPos = -1
Do While Selection.Find.Execute = True
    If lastPos > Selection.Start Then Exit Do
    Selection.EndKey Unit:=wdLine
    Selection.TypeParagraph
Loop
user2903089
  • 293
  • 1
  • 10
0

Add Selection.Find.Execute Replace:=wdReplaceAll after your End with

Dave F
  • 151
  • 14