Word Macro: Deleting an Active Page

To delete a page from a Word document is a little annoying. Because, we need to select everything on the page.
So, I’ve made this Word VBA macro.
This macro deletes an active page from the active Word document without selecting everything.

Macro Example

Sub DeleteActivePage()
 
  ActiveDocument.Bookmarks("\Page").Range.Delete

End Sub

Description

We need only the following code:

  ActiveDocument.Bookmarks("\Page").Range.Delete

That’s all, but this object model hierarchy is a little deep.
I will explain about that.

(Application.)ActiveDocument

  ActiveDocument...

The Application.ActiveDocument property returns an active Document object.

obj-Word-Application-ActiveDocument

And the ActiveDocument property is a member of the Word.Global class, so we can omit the Application property.

obj-Word-ActiveDocument

ActiveDocument.Bookmarks (Document.Bookmarks)

  ActiveDocument.Bookmarks...

The ActiveDocument.Bookmarks (Document.Bookmarks property) returns a Bookmarks collection object that represents all the bookmarks in the active document.

obj-Word-Document-Bookmarks

Bookmarks(“\Page”) (Bookmarks.Item)

  ActiveDocument.Bookmarks("\Page")...

The Bookmarks(“\Page”) (Bookmarks.Item method) returns an individual Bookmark object from the Bookmarks collection.

obj-Word-Bookmarks-Item

Because the Item method is a default member of the Bookmarks collection object, we can omit the .Item.

So, the Bookmarks(“\Page”) is equal Bookmarks.Item(“\Page”).

And the Bookmarks(“\Page”) returns the active page of the document by the predefined bookmark named “\Page”.

Bookmarks(“\Page”).Range (Bookmark.Range)

  ActiveDocument.Bookmarks("\Page").Range...

The Bookmark.Range property returns the Range object that represents the portion of a document that is contained in the bookmark. In this macro the Bookmarks(“\Page”).Range returns the active page.

obj-Word-Bookmark-Range

Range.Delete

Finally, we can delete the active page by the Range.Delete method.

Sub DeleteActivePage()

  ActiveDocument.Bookmarks("\Page").Range.Delete

End Sub

obj-Word-Range-Delete

Properties

http://www.relief.jp/itnote/archives/word-vba-delete-page.php

Apply To

  • Word 2013
  • Word 2010
  • Word 2007