E
Ed
It's supposed to be a simple loop: find text, do this 'n that, find the next
one and do it again. But it doesn't find the next one - it keeps going back
to the first one! One of the commands is inserting a page break, so I've
got 23,000 new pages inserted before the first text string! How do I fix
this so it goes down and stops at the end of the document?
Ed
Sub sac2()
Dim oRg As Range
Set oRg = ActiveDocument.Range
With oRg.Find
.ClearFormatting
.Text = "ITEM ID"
.Forward = True ' this says "Search Forward Only", right?
.Format = False
.Wrap = wdFindStop ' this says "Stop at the End", right?
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
Do While .Execute(FindText:="ITEM ID") = True
oRg.Words(1).Select
' Remove unwanted spaces to correctly position the text
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.TypeText Text:=" "
' Add a page break before the text
Selection.HomeKey Unit:=wdLine
Selection.TypeParagraph
Selection.InsertBreak Type:=wdPageBreak
Selection.MoveDown Unit:=wdLine, Count:=2
Loop
End With
End Sub
one and do it again. But it doesn't find the next one - it keeps going back
to the first one! One of the commands is inserting a page break, so I've
got 23,000 new pages inserted before the first text string! How do I fix
this so it goes down and stops at the end of the document?
Ed
Sub sac2()
Dim oRg As Range
Set oRg = ActiveDocument.Range
With oRg.Find
.ClearFormatting
.Text = "ITEM ID"
.Forward = True ' this says "Search Forward Only", right?
.Format = False
.Wrap = wdFindStop ' this says "Stop at the End", right?
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
Do While .Execute(FindText:="ITEM ID") = True
oRg.Words(1).Select
' Remove unwanted spaces to correctly position the text
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.TypeText Text:=" "
' Add a page break before the text
Selection.HomeKey Unit:=wdLine
Selection.TypeParagraph
Selection.InsertBreak Type:=wdPageBreak
Selection.MoveDown Unit:=wdLine, Count:=2
Loop
End With
End Sub