Word Macro: Deleting All Tables from an Active Document

Sometimes I have to delete many tables from a Word document. It’s very annoying.
So, I’ve made this Word VBA macro.

This macro allows us to delete all the tables from an active document.

Macro Example

Sub DeleteTables_ActiveDocument()

  Dim tbl As Table

  For Each tbl In ActiveDocument.Tables
    tbl.Delete
  Next tbl

End Sub

Description

Variable Declaration

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

Sub...

  Dim tbl As Table

The Table object represents a single table in a document.

obj-Word-Table

Starts Looping

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

Sub...

  Dim tbl As Table

  For Each tbl In ActiveDocument.Tables

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

The Document.Tables property returns a Tables collection object.

obj-Word-Document-Tables

The Tables is a collection of a single Table objects.
So, the Tables collection object represents the tables in a selection, range, or document.

obj-Word-Tables

Delete the Table

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

Sub...

  Dim tbl As Table

  For Each tbl In ActiveDocument.Tables
    tbl.Delete

obj-Word-Table-Delete

Loops Back

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

Sub...

  Dim tbl As Table

  For Each tbl In ActiveDocument.Tables
    tbl.Delete
  Next tbl

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

Sub DeleteTables_ActiveDocument()

  Dim tbl As Table

  For Each tbl In ActiveDocument.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