MS Word - Macro to delete last 3 pages of document

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
AndresHernandoAsked:
Who is Participating?
 
GrahamSkanConnect With a Mentor RetiredCommented:
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
        .Bookmarks("Instructions").Range.Delete
        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.
DelIns.doc
0
 
AndresHernandoAuthor Commented:
...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.
0
 
sjklein42Commented:
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

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.

All Courses

From novice to tech pro — start learning today.