Learn how to a build a cloud-first strategyRegister Now


MS Word - Macro to delete last 3 pages of document

Posted on 2011-10-20
Medium Priority
Last Modified: 2012-06-21
I have a 5-page MS Word document and want to put in a button or other mechanism that will allow the user to delete the last 3 pages of the document, which are instructions, leaving only the first two pages.

Thanks, --Andres
Question by:AndresHernando

Author Comment

ID: 37004764
...note:  The document has "Editing Restrictions": Allow only this type of editing in the document: Filling in forms.   This means that the VBA code must temporarily removethe editing restriction.
LVL 16

Expert Comment

ID: 37004794
Not sure how to handle the editing restrictions, but a sequence like this will delete all but the first three pages of a document.  Jump to page 4, back up a character, select through to end-of-document, and delete selection.

    Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="4"
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.EndKey Unit:=wdStory, Extend:=wdExtend
    Selection.Delete Unit:=wdCharacter, Count:=1

Open in new window

LVL 76

Accepted Solution

GrahamSkan earned 2000 total points
ID: 37005328
I suggest that you bookmark the part to be deleted. That ensures that any repagination will not affect the outcome.

You can then use a macro like this.
Sub DeleteInstructons()
    With ActiveDocument
        .Unprotect 'MyPassword
        MsgBox "Instructions Deleted"
        .Protect wdAllowOnlyReading ', MyPassword
    End With
End Sub

Open in new window

After it is written, you can put a MacroButton field on the document to call it. Here is a demonstration document.

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.
Suggested Courses
Course of the Month21 days, 7 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question