Avatar of Andreas Hermle
Andreas Hermle
Flag for Germany asked on

delete blank pages by means of VBA

Dear Experts:

I got  a huge decument with more than 600 pages ...
... where there are empty pages that are a result of a manual page break (the page break being at the very top of the page).

 blank_page_resulting_from_page_break.tif

If I delete this page break manually, an empty paragraph mark remains which also needs to be deleted.

Could somebody please come up with a code that deletes all these empty pages along with the ensuing blank paragraph mark.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
VBAMicrosoft WordMicrosoft Office

Avatar of undefined
Last Comment
Andreas Hermle

8/22/2022 - Mon
Shums Faruk

Please try below:
Public Sub DeleteBlankPage()
Selection.GoTo What:=wdGoToBookmark, Name:="\page"
If isBlankSelection Then
Selection.Delete
End If
End Sub
Public Function BlankPageSelection()
For Each c In Selection.Characters
If (c vbCr And c vbTab And c vbFormFeed And c " ") Then
BlankPageSelection = False
Exit Function
End If
Next
BlankPageSelection = True
End Function

Open in new window

Andreas Hermle

ASKER
Hi Shums,

thank you very much for your professional support. I am afraid to say that the code is not working. Only one of these page breaks get selected, not the others and nothing is deleted.

Thank you
Shums Faruk

Thanks Andreas for coming back, please try below:
Sub DeleteEmptyPages(ByVal Worddoc As Word.Document)
With Worddoc.Content.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = ChrW(12)
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
End With
End Sub

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Andreas Hermle

ASKER
Hi Shums, thank you very much for it, regrettably it does not work. Nothing happens.

Regards, Andreas
ASKER CERTIFIED SOLUTION
Shums Faruk

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Andreas Hermle

ASKER
Hi Shums,
every page break gets deleted with above code, I only would like to delete the ones which are at the very top of a blank page.

Thank you. Regards, Andreas
Shums Faruk

Sorry Andreas,

May be some other expert can help you, initially you asked
"Could somebody please come up with a code that deletes all these empty pages along with the ensuing blank paragraph mark."
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Andreas Hermle

ASKER
No problem Shums, you have already helped me a lot of times in the excel section. Thank you very much for your efforts.
Shums Faruk

You're Welcome Andreas! I wish I have helped you with Word as well.
Thank you for noticing my help in Excel.
Andreas Hermle

ASKER
Hi Shums,

this code works fine in a couple of circumstances, which I also have off and on. Therefore this solution is fine for me.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy