Answered by:
Word 2013 Macro to insert a page break after every other text "Date"

Question
-
Good morning,
I am trying to write a macro to insert a page break after every other word it sees called "Date" Each section has 2 texts "Date" and i need the macro to insert after the 1st time it recognizes it. I wrote this to insert at every text "Date"
Selection.HomeKey Unit:=wdStory
With Selection.Find
.Text = "Date"
.Forward = True
End With
Do
Selection.Find.Execute
If Selection.Find.Found = True Then
Selection.InsertBreak Type:=wdPageBreak
End If
Loop While Selection.Find.Found = True
End SubAny help would be greatly appreciated. Can you also tell me how to have it insert at the test "Date" and not replace the word with a page break?
Thank you,
Monday, November 21, 2016 1:23 PM
Answers
-
It almost works, only issue is it is putting the page break after the text"Date".
That is literally what you asked for:
a macro to insert a page break after every other word it sees called "Date" ... and not replace the word with a page break?
If you want to insert the page break before the word "Date":
Sub InsertBreaks() Dim lngCounter As Long Application.ScreenUpdating = False Selection.HomeKey Unit:=wdStory With Selection.Find .Text = "Date" .ClearFormatting .Forward = True .Wrap = wdFindStop Do While .Execute lngCounter = lngCounter + 1 If lngCounter Mod 2 = 1 Then Selection.Collapse Direction:=wdCollapseStart Selection.InsertBreak Type:=wdPageBreak Selection.MoveRight Count:=5 End If Loop End With Application.ScreenUpdating = True End Sub
Regards, Hans Vogelaar (http://www.eileenslounge.com)
- Proposed as answer by Deepak Saradkumar PanchalMicrosoft contingent staff Tuesday, November 22, 2016 4:58 AM
- Marked as answer by WMiller0220 Monday, January 2, 2017 1:09 PM
Monday, November 21, 2016 8:51 PM
All replies
-
Try this version:
Sub InsertBreaks() Dim lngCounter As Long Application.ScreenUpdating = False Selection.HomeKey Unit:=wdStory With Selection.Find .Text = "Date" .ClearFormatting .Forward = True .Wrap = wdFindStop Do While .Execute lngCounter = lngCounter + 1 If lngCounter Mod 2 = 1 Then Selection.Collapse Direction:=wdCollapseEnd Selection.InsertBreak Type:=wdPageBreak End If Loop End With Application.ScreenUpdating = True End Sub
Regards, Hans Vogelaar (http://www.eileenslounge.com)
Monday, November 21, 2016 3:43 PM -
It almost works, only issue is it is putting the page break after the text"Date". Is there a way to move it 5 spots left or remove text "Date" completely once the page break is inserted?
Thank you again for all your help!
Monday, November 21, 2016 4:01 PM -
It almost works, only issue is it is putting the page break after the text"Date".
That is literally what you asked for:
a macro to insert a page break after every other word it sees called "Date" ... and not replace the word with a page break?
If you want to insert the page break before the word "Date":
Sub InsertBreaks() Dim lngCounter As Long Application.ScreenUpdating = False Selection.HomeKey Unit:=wdStory With Selection.Find .Text = "Date" .ClearFormatting .Forward = True .Wrap = wdFindStop Do While .Execute lngCounter = lngCounter + 1 If lngCounter Mod 2 = 1 Then Selection.Collapse Direction:=wdCollapseStart Selection.InsertBreak Type:=wdPageBreak Selection.MoveRight Count:=5 End If Loop End With Application.ScreenUpdating = True End Sub
Regards, Hans Vogelaar (http://www.eileenslounge.com)
- Proposed as answer by Deepak Saradkumar PanchalMicrosoft contingent staff Tuesday, November 22, 2016 4:58 AM
- Marked as answer by WMiller0220 Monday, January 2, 2017 1:09 PM
Monday, November 21, 2016 8:51 PM -
Good morning,
Thank you for all of your help. I have 1 more question with regards to similar more simplistic macro. It puts a page break after the word "Date" instead of before. Any assistance would be greatly appreciated.
Here is the code for that:
Sub PageBreak()
'
' Insert Page Breaks
'
'
Selection.HomeKey Unit:=wdStory
With Selection.Find
.Text = "Date"
.Forward = True
End With
Do
Selection.Find.Execute
If Selection.Find.Found = True Then
Selection.InsertBreak Type:=wdPageBreak
End If
Loop While Selection.Find.Found = True
End SubMonday, January 2, 2017 1:09 PM -
You could use this:
Sub PageBreak() ' ' Insert Page Breaks ' ' Selection.HomeKey Unit:=wdStory With Selection.Find .Text = "Date" .Forward = True Do While .Execute Selection.Collapse Selection.InsertBreak Type:=wdPageBreak Selection.MoveRight Count:=4 Loop End With End Sub
Regards, Hans Vogelaar (http://www.eileenslounge.com)
Monday, January 2, 2017 6:11 PM