Code to add page breaks

D

DJENSE00

Hi everyone...my first foray into this group. Hope someone can help!

I have an .rtf file that I am opening in Word. It is a statistical
output summary and needs a bit of reformatting.

Basically, the end of each intended page has a footer that has a text
string that says "ANOVA Results". After that text string I want to
include a page break. There are anywhere from 1 to 50 such page
breaks that need to be inserted - I do not know ahead of time. Can
someone help me loop through this entire document, searching for this
text string and then inserting a page break. By the way, I have
already figured out how to insert the page break in VBA at the
appropriate place but I simply cannot figure out how to loop through
the entire document looking for each instance of this page footer. My
code currently looks like this:

Sub InsertPageBreaks()

'Find instances of page footers
Do Until ActiveDocument.Bookmarks("\Sel") =
ActiveDocument.Bookmarks("\EndOfDoc")
Selection.Find.ClearFormatting
With Selection.Find
.Text = "* ANOVA with Dunnett's/Dunn's (p <= 0.05)"
.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
'After finding the instance of the search string move down
one line
'Selection.TypeText Text:=" "
Selection.MoveDown Unit:=wdLine, Count:=1
'Set a page break at this point and then delete the next
line which is blank
Selection.InsertBreak Type:=wdPageBreak
Selection.Delete Unit:=wdCharacter, Count:=1
Else
Exit Do
End If
Loop

End Sub

The loop never terminates and I have to break out of it.
Any help would be more than greatly appreciated. Probably a simple
answer for someone who knows more than me - i.e. almost anyone!!! :)

Thanks!!!!
 
G

Greg Maxey

Provided your ANOVA Results terminates with a paragraph mark this
should do:

Sub InsertPageBreaks()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "ANOVA"
While .Execute
With oRng
.MoveEndUntil Cset:=vbCr, Count:=wdForward
.MoveEnd wdCharacter, 1
.Collapse wdCollapseEnd
.InsertBreak Type:=wdPageBreak
End With
Wend
End With
End Sub
 
D

Doug Robbins - Word MVP

Use:

Dim myrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="ANOVA Results", Forward:=True,
Wrap:=wdFindStop) = True
Set myrange = Selection.Range
myrange.Collapse wdCollapseEnd
myrange.InsertBreak
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
 
G

Greg Maxey

Doug,

I don't know, but from his example it looks like "Results" is some
varialbe text?
 
D

Doug Robbins - Word MVP

Hi Greg,

Looking at his code you are probably right. In that case I would use:

Dim myrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="ANOVA Results", Forward:=True,
Wrap:=wdFindStop) = True
Set myrange = Selection.Paragraphs(1).Range
myrange.Collapse wdCollapseEnd
myrange.InsertBreak
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
 
G

Greg Maxey

Doug,

I like your clean approach. It probably does, but I am not sure that
ANOVA starts a paragraph ;-)
 
D

Doug Robbins - Word MVP

Hi Greg,

It would not matter where ANOVA was in the paragraph.

--
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
 
G

Greg Maxey

Well I could just crawl off in a corner and die or shout your supremacy with
VBA. I think I will shout. All I know I have learned from folks like you
so I simply learn a little more. Thanks Doug.
 

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