Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / April 2007

Tip: Looking for answers? Try searching our database.

Code to add page breaks

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DJENSE00@YAHOO.COM - 30 Mar 2007 18:29 GMT
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!!!!
Greg Maxey - 30 Mar 2007 19:58 GMT
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

On Mar 30, 1:29 pm, "DJENS...@YAHOO.COM" <DJENS...@YAHOO.COM> wrote:
> Hi everyone...my first foray into this group.  Hope someone can help!
>
[quoted text clipped - 53 lines]
>
> Thanks!!!!
Doug Robbins - Word MVP - 30 Mar 2007 20:00 GMT
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

Signature

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

> Hi everyone...my first foray into this group.  Hope someone can help!
>
[quoted text clipped - 53 lines]
>
> Thanks!!!!
Greg Maxey - 30 Mar 2007 20:07 GMT
Doug,

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

On Mar 30, 3:00 pm, "Doug Robbins - Word MVP"
<d...@REMOVECAPSmvps.org> wrote:
> Use:
>
[quoted text clipped - 81 lines]
>
> - Show quoted text -
Doug Robbins - Word MVP - 31 Mar 2007 07:03 GMT
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

Signature

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

> Doug,
>
[quoted text clipped - 88 lines]
>>
>> - Show quoted text -
Greg Maxey - 01 Apr 2007 05:53 GMT
Doug,

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

On Mar 31, 2:03 am, "Doug Robbins - Word MVP"
<d...@REMOVECAPSmvps.org> wrote:
> Hi Greg,
>
[quoted text clipped - 114 lines]
>
> - Show quoted text -
Doug Robbins - Word MVP - 01 Apr 2007 06:41 GMT
Hi Greg,

It would not matter where ANOVA was in the paragraph.

Signature

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

> Doug,
>
[quoted text clipped - 126 lines]
>>
>> - Show quoted text -
Greg Maxey - 01 Apr 2007 07:04 GMT
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.

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Hi Greg,
>
[quoted text clipped - 130 lines]
>>>
>>> - Show quoted text -
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.