Link to home
Start Free TrialLog in
Avatar of Andreas Hermle
Andreas HermleFlag 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
Avatar of Shums Faruk
Shums Faruk
Flag of India image

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

Avatar of 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
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

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

Regards, Andreas
ASKER CERTIFIED SOLUTION
Avatar of Shums Faruk
Shums Faruk
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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."
No problem Shums, you have already helped me a lot of times in the excel section. Thank you very much for your efforts.
You're Welcome Andreas! I wish I have helped you with Word as well.
Thank you for noticing my help in Excel.
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.