Loop through a document from beginning to end

J

Jim Houston

How can I get a macro to start at the beginning of a document and repeat
until it reaches the end? What I need to do is search for a certain string
and insert a page break before each instance of the string. What I need is
something like:
Do while not document.end
other code
loop

thanks in advance

Jim
 
J

Jay Freedman

How can I get a macro to start at the beginning of a document and repeat
until it reaches the end? What I need to do is search for a certain string
and insert a page break before each instance of the string. What I need is
something like:
Do while not document.end
other code
loop

thanks in advance

Jim

Looping is not a good way to do this. The better choice is a Replace operation,
specifying to replace all instances. In the following sample, the code ^m means
a manual page break, and the code ^& means the text that was found.

Sub demo()
Dim oRg As Range
Set oRg = ActiveDocument.Range
With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "This is the text to find"
.Replacement.Text = "^m^&"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = False
.Execute Replace:=wdReplaceAll
End With
End Sub

Note that you can do this in the Edit > Replace dialog, without needing a macro.

If the string you're looking for has a unique style applied to each occurrence,
and not to any other text, then there's an even simpler solution that also
doesn't involve a macro: just modify that style to include the Page Break Before
attribute.
 
J

Jim Houston

Jay
Thanks for the help. Just one more question - is there any way to get Word
to skip the first instance of the string? In other words, I only want to
start inserting page breaks at the beginning of the 2nd instance of the
string.
 
F

fumei via OfficeKB.com

One way would be to put the Selection after the first instance, and use
Selection.Find (with Forward); OR make the range object from the Selection
forward to the end of the document.

There could be other ways but they would use a Loop and a counter, and Jay is
of course correct, looping is slow.

If you are doing it "manually" - using Find and Replace - simply use Find
Next to get to the first instance, then Find Next again to get to the second
instance, then click Replace All.

Jim said:
Jay
Thanks for the help. Just one more question - is there any way to get Word
to skip the first instance of the string? In other words, I only want to
start inserting page breaks at the beginning of the 2nd instance of the
string.
On Thu, 1 May 2008 17:36:43 -0700, "Jim Houston"
<[email protected]>
[quoted text clipped - 52 lines]
Email cannot be acknowledged; please post all follow-ups to the newsgroup
so all may benefit.
 

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