Consulting

Results 1 to 4 of 4

Thread: Removing blank pages

  1. #1
    VBAX Regular
    Joined
    May 2015
    Posts
    43
    Location

    Removing blank pages

    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

  2. #2
    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.
    Graham Mayor - MS MVP (Word)
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    May 2015
    Posts
    43
    Location
    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

  4. #4
    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
    Graham Mayor - MS MVP (Word)
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •