PDA

View Full Version : [SOLVED:] Extra Manual Page Breaks



JeremyS
07-06-2014, 04:56 PM
I have a code that removes tables that are not necessary from a word 2010 document.

However, there are occasions where I am being left with two manual page breaks. This means I have a blank page.

What I want is a single page break at the end of the text.

I am struggling to write a code that will search the active document and replace 2 or more manual page breaks with 1 page break so that there are no blank pages.

Can anyone assist?

Thanks for your help.

macropod
07-06-2014, 11:37 PM
You should be able to do with a wildcard Find/Replace, where:
Find = [^12]{2,}
Replace = ^12
Of course, this assumes there is nothing else (not even an empty paragraph) between those page breaks. You might want to precede the above Find/Replace with:
Find = [^12][^13^l^t ]{1,}[^12]
Replace = ^12

JeremyS
07-07-2014, 12:26 AM
Paul,

Thank you for the assistance, but clearly I am still just learning!

Sub Para()

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = [^12]{2,}
.Replacement.Text = ^12
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

End Sub

macropod
07-07-2014, 12:41 AM
There are some errors in your code (e.g. omission of quotes enclosing the Find/Replace expressions), it's not using the Wildcards option, even though the Find expression requires it, and it's missing a key ingredient - the .Execute statement! Try:

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "[^12]{2,}"
.Replacement.Text = "^12"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub
PS: When posting code, please use the code tags. They're indicated by the # button on the posting menu.

JeremyS
07-07-2014, 01:18 AM
Thanks for the help!

If there are multiple pages, It looks like I may have to run this macro multiple times. I setup a loop and it worked!

Thanks!

macropod
07-07-2014, 02:07 AM
If there are multiple pages, It looks like I may have to run this macro multiple times. I setup a loop and it worked!
You might have set up a loop - but it didn't 'work' because it isn't needed. The code I posted is quite sufficient as is.

sernest
01-04-2019, 02:29 PM
Thanks for this.