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 -