Link to home
Start Free TrialLog in
Avatar of rrhandle8
rrhandle8Flag for United States of America

asked on

Word VBA -- Delete last page

I have code that creates a document and fills it with data.  The last page is always blank. How do I go to the last page and delete it?
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

This VBA macro should delete the last page of the active document:
Sub DelLastPage()
    ActiveDocument.Bookmarks("\EndOfDoc").Range.Select
    ActiveDocument.Bookmarks("\Page").Range.Delete
End Sub

Open in new window

Avatar of rrhandle8

ASKER

GrahamSkan,  Did not work.  I wonder if it is because there is a paragraph mark on the last page?
Try this
Sub DeleteLastPage()
Dim lngCharacters As Long
Dim r As Range

With ActiveDocument
    lngCharacters = .GoTo(wdGoToPage, wdGoToLast).Start
    Set r = .Range(lngCharacters - 1, .Range.End)
    r.Delete
End With
End Sub

Open in new window

Martin Liss, error in code
I made a change while you were probably testing. Copy it again. If you still get an error then please tell me the line that causes the error and what the error is.
Set r = .Range(lngCharacters - 1, .Range.End)

Value out of range error
Please give this a try...

Sub DeleteLastPage()
Dim doc As Document
Dim Rng As Range

Set doc = ActiveDocument

With doc
    Set Rng = .GoTo(wdGoToPage, wdGoToLast)
    Set Rng = .Range(Rng.Start - 1, .Characters.Count)
    Rng.Delete
End With
End Sub

Open in new window

Can you attach your document?
Here is the document
Table1.docx
Did you try the code I posted here?
Set Rng = .Range(Rng.Start - 1, .Characters.Count)

value out of range error
I ran the code in your document and it ran successfully and deleted the 3rd page from the document.
Please refer to the attached and run the code.
And you will get the error you reported only if the document has only one page.
Table1-1.docm
Subodh Tiwari (Neeraj), ?????? I believe you, but I am still getting the same error.
What version you are using?
2010
This works for me in Word 2010 but it is a solution pretty specific to your document. Why not just do it manually?

Sub DeleteLastPage()
Dim doc As Document
Dim Rng As Range

Set doc = ActiveDocument

With doc
    Set Rng = .GoTo(wdGoToPage, wdGoToLast)
    Set Rng = .Range(Rng.Start - 1, .Characters.Count)
    Rng.Delete
    
    Set Rng = .GoTo(wdGoToPage, wdGoToLast)
    Set Rng = .Range(Rng.Start - 1, .Characters.Count)
    Rng.Delete
End With
End Sub

Open in new window

Exactly the same problem:
Set Rng = .Range(Rng.Start - 1, .Characters.Count)
value out of range error

Why not do it manually? Because this is part of a much larger batch job that creates many documents.  When the batch is done, the documents must be ready to go.
I've attached the document that works for me. Go to Visual Basic by entering Alt+f11 and run the DeleteLastPage macro.
29067732.docm
Martin,
Same error as before.

I found this on-line, and it works fine.
Sub DeleteAfterLastPage()

Dim rng As Range
Dim pageCount As Integer

' work out how many pages
pageCount = ActiveDocument.ComputeStatistics(wdStatisticPages)

' set a range from last page to the end
Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=pageCount
Set rng = Selection.Range
Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=pageCount
rng.End = Selection.Bookmarks("\Page").Range.End

' delete the range
rng.Delete

' optionally remove the left over blank page
Selection.TypeBackspace
Selection.TypeBackspace

End Sub

Open in new window

But it only works if the page is blank. In my case, that is what I have, so it works, but I have still not gotten an answer to the question how to delete the last page blank or not.
Same error as before.
I can't imagine why using the same file and the same version of Word that you get an error and I don't.
That is a very good question. Do you think it might be a reference?
2017-11-10_19-38-21.png
My references are exactly the same as yours. Is the document that you attached (and the one in the document I attached) the only one open?
Yes, it was the only one open.
Using the following code, put a breakpoint on line 9 by clicking in the left-hand margin. (A red dot will appear there.) Then run the code and when the code gets there, hover over Rng.Start and tell me what it says. In my case it says "Rng.Start = 173". Do the same for .Characters.Count and tell me what that says too.
Sub DeleteLastPage()
Dim doc As Document
Dim Rng As Range

Set doc = ActiveDocument

With doc
    Set Rng = .GoTo(wdGoToPage, wdGoToLast)
    Set Rng = .Range(Rng.Start - 1, .Characters.Count)
    Rng.Delete

End With
End Sub

Open in new window

Rng.Start = 172
Charger.Count = 174
I am not getting the error I got yesterday.
If the only thing on the last page is a paragraph mark, the page is deleted.
If there is more than a paragraph mark on the page, everything is deleted except for one paragraph mark, so the page is still there.
If the only thing on the last page is a paragraph mark, the page is deleted
I assume that's good.
If there is more than a paragraph mark on the page, everything is deleted except for one paragraph mark, so the page is still there.
That doesn't happen for me when I add some paragraphs to the last page, so please attach a document that has that condition.
See attached
29067732-B.docm
ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America 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
This is strange.  If paragraph marks are turned ON, your code works.  If the paragraph marks are not turned ON, the code does not work.
If the paragraph marks are turned OFF, and the first line of the code is to turn them ON, then the next line of code is your code to turn them OFF, it works. I'll take this as an acceptable, but confusing, answer.

Thank you!

Option Explicit
Sub DeleteLastPage()
Dim doc As Document
Dim rng As Range
Dim par As Paragraph
Set doc = ActiveDocument
Dim i As Integer


ActiveWindow.ActivePane.View.ShowAll = Not ActiveWindow.ActivePane.View.ShowAll

If ActiveWindow.ActivePane.View.ShowAll Then
    ActiveWindow.ActivePane.View.ShowAll = Not ActiveWindow.ActivePane.View.ShowAll
End If

With doc

    On Error Resume Next
    Set rng = .GoTo(wdGoToPage, wdGoToLast)
    Set rng = .Range(rng.Start - 1, .Characters.Count)
    rng.Delete

    For Each par In .Paragraphs
        If Len(par.Range.Text) <= 1 Then
            par.Range.Delete
        End If
    Next par

    Set rng = .GoTo(wdGoToPage, wdGoToLast)
    Set rng = .Range(rng.Start - 1, .Characters.Count)
    rng.Delete

End With
End Sub

Open in new window

You're welcome and I'm glad I was able to help.

If you expand the “Full Biography” section of my profile you'll find links to some articles I've written that may interest you.

Marty - Microsoft MVP 2009 to 2017
              Experts Exchange MVE 2015
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2017