Word Macro: Deleting Tables from an Active Page

I’ve explained a Word VBA macro deleting all tables from an active Word document.
Today, I will explain how to delete tables from an active page.

Macro Example

Sub DeleteTables_ActivePage()

  Dim tbl As Table

  For Each tbl In _
  ActiveDocument.Bookmarks("\Page").Range.Tables
    tbl.Delete
  Next tbl

End Sub

Description

Variable Declaration

Sub...

  Dim tbl As Table

The 3rd line declares an object variable named tbl that refers to a Table object.

obj-Word-Table

The Table object represents a single table.

Starts Looping

The 5th-6th line start to loop through all the elements (Table) of a collection (Tables).

Sub...

  Dim tbl As Table

  For Each tbl In _
  ActiveDocument.Bookmarks("\Page").Range.Tables

ActiveDocument property

  ActiveDocument...

The Application.ActiveDocument property returns an active Document object.

obj-Word-Application-ActiveDocument

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

obj-Word-ActiveDocument

Document.Bookmarks property

  ActiveDocument.Bookmarks...

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

obj-Word-Document-Bookmarks

Bookmarks.Item method

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

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

obj-Word-Bookmarks-Item

The .Item method is a default member of the Bookmarks collection object. We can see a little blue marble beside its icon in the Object Browser.
So, we can omit the .Item.
Well, the code:
.Bookmarks(“\Page”) is equal
.Bookmarks.Item(“\Page”)
And the .Bookmarks(“\Page”) returns an active page by the predefined bookmark named “\Page” .

Bookmark.Range property

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

The Bookmark.Range property (Bookmarks(“\Page”).Range) returns a Range object that represents the range or portion of a document that is contained in the bookmark.

obj-Word-Bookmark-Range

Range.Tables property

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

The Range.Tables property returns a Tables collection object that represents all the tables in a range.

obj-Word-Range-Tables

Delete the Table

Sub...

  Dim tbl As Table

  For Each tbl In _
  ActiveDocument.Bookmarks("\Page").Range.Tables
    tbl.Delete

The 7th line deletes the table by the Table.Delete method.

obj-Word-Table-Delete

Loops Back

The 8th line loops back to get the next table.

Sub...

  Dim tbl As Table

  For Each tbl In _
  ActiveDocument.Bookmarks("\Page").Range.Tables
    tbl.Delete
  Next tbl

After every table is deleted from the active document, this macro ends.

Sub DeleteTables_ActivePage()

  Dim tbl As Table

  For Each tbl In _
  ActiveDocument.Bookmarks("\Page").Range.Tables
    tbl.Delete
  Next tbl

End Sub

Properties

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

Apply To

  • Word 2013
  • Word 2010
  • Word 2007