MS Word - Macro to delete last 3 pages of document

Posted on 2011-10-20
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

    ...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

    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

    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

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Like many others, we try and discourage users from printing documents unnecessarily and instead send or share them electronically. However, this doesn't always work and documents are still printed. With this simple solution, if the user tries to …
    It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
    This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
    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.

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now