• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 587
  • Last Modified:

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?
0
rrhandle8
Asked:
rrhandle8
  • 16
  • 11
  • 4
  • +1
1 Solution
 
GrahamSkanRetiredCommented:
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

0
 
rrhandle8Author Commented:
GrahamSkan,  Did not work.  I wonder if it is because there is a paragraph mark on the last page?
0
 
Martin LissOlder than dirtCommented:
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

0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
rrhandle8Author Commented:
Martin Liss, error in code
0
 
Martin LissOlder than dirtCommented:
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.
0
 
rrhandle8Author Commented:
Set r = .Range(lngCharacters - 1, .Range.End)

Value out of range error
0
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
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

0
 
Martin LissOlder than dirtCommented:
Can you attach your document?
0
 
rrhandle8Author Commented:
Here is the document
Table1.docx
0
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Did you try the code I posted here?
0
 
rrhandle8Author Commented:
Set Rng = .Range(Rng.Start - 1, .Characters.Count)

value out of range error
0
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
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
0
 
rrhandle8Author Commented:
Subodh Tiwari (Neeraj), ?????? I believe you, but I am still getting the same error.
0
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
What version you are using?
0
 
rrhandle8Author Commented:
2010
0
 
Martin LissOlder than dirtCommented:
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

0
 
rrhandle8Author Commented:
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.
0
 
Martin LissOlder than dirtCommented:
I've attached the document that works for me. Go to Visual Basic by entering Alt+f11 and run the DeleteLastPage macro.
29067732.docm
0
 
rrhandle8Author Commented:
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

0
 
rrhandle8Author Commented:
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.
0
 
Martin LissOlder than dirtCommented:
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.
0
 
rrhandle8Author Commented:
That is a very good question. Do you think it might be a reference?
2017-11-10_19-38-21.png
0
 
Martin LissOlder than dirtCommented:
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?
0
 
rrhandle8Author Commented:
Yes, it was the only one open.
0
 
Martin LissOlder than dirtCommented:
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

0
 
rrhandle8Author Commented:
Rng.Start = 172
Charger.Count = 174
0
 
rrhandle8Author Commented:
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.
0
 
Martin LissOlder than dirtCommented:
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.
0
 
rrhandle8Author Commented:
See attached
29067732-B.docm
0
 
Martin LissOlder than dirtCommented:
When I first tried it with your new document the paragraph marks, etc. were showing and I got the out of range error! When I turned them off I didn't. Try this code which first turns them off if they're on.

Sub DeleteLastPage()
Dim doc As Document
Dim rng As Range
Dim par As Paragraph
Set doc = ActiveDocument

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

With doc
    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

0
 
rrhandle8Author Commented:
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

0
 
Martin LissOlder than dirtCommented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 16
  • 11
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now