PDA

View Full Version : Removing blank pages



ajhez
08-14-2017, 03:08 AM
Hi all,

I have a document generated by userform - as a lot of text is deleted due to choices made by the user in the userform, it often leaves blank pages at the end of the document.

Does anyone have any experience of deleting blank pages like this? Ideally I would need to be able to delete blank pages up until the point that there is a page with text. It would run at the end of the userform to essentially clean up the doc.


Public Function DeleteBlankPages(wd As Word.Document)
Dim par As Paragraph
For Each par In wd.Paragraphs
If Len(par.Range.Text) <= 1 Then
par.Range.Delete
End If
Next par
End Function

I found the above online, but doesn't seem to work for me...

Any thoughts would be gratefully received.

Thanks,
AJHEZ

gmayor
08-14-2017, 04:32 AM
There are no pages in a Word document, so in order to know how to deal with the empty 'pages' we would need to know what was creating the space. You could start by pressing the ¶ button on the Home tab of the ribbon, which might provide some indication of the issue.

ajhez
08-14-2017, 06:00 AM
Thanks, Graham - good point!

It looks like primarily a number of returns are contained at the end of the document (where previous text has been deleted by the userform) - there is also a pagebreak in one of the 'pages' which would also need to be deleted.

Is it possible to do something like backspace from the final point in the document up until a character or table is reached?

Any ideas would be great.

Thanks,
AJHEZ

gmayor
08-15-2017, 12:41 AM
If they are all at the end then


Sub Macro1()
Dim oRng As Range
Set oRng = ActiveDocument.Range
oRng.Collapse 0
oRng.MoveStartWhile Chr(13), wdBackward
oRng.Delete
lbl_Exit:
Set oRng = Nothing
Exit Sub
End Sub

see also http://www.gmayor.com/replace_using_wildcards.htm