Word VBA: Check If a Table Exists in Active Document

This Word VBA macro checks if a table exists in an active document.

Macro Example

Sub CheckIfTableExist_ActiveDocument()

  If ActiveDocument.Tables.Count = 0 Then
    MsgBox "Table NOT Exists."
  Else
    MsgBox "Table Exists!"
  End If

End Sub

Description

Checks If a Table Exists

Sub...

  If ActiveDocument.Tables.Count = 0 Then

The 3rd line checks whether a table exists in an active document.

[Application.]ActiveDocument

  If ActiveDocument...

The [Application.]ActiveDocument property returns an active Document object.

obj-Word-Application-ActiveDocument

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

obj-Word-ActiveDocument

ActiveDocument Tables (Document.Tables)

  If ActiveDocument.Tables.Count = 0 Then

The ActiveDocument.Tables (Document.Tables property) returns the Tables collection object in the active document.

obj-Word-Document-Tables

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

obj-Word-Tables

Tables.Count

  If ActiveDocument.Tables.Count...

The Tables.Count property returns the long integer value that represents tables count.

Not Exists

Sub...

  If ActiveDocument.Tables.Count = 0 Then
    MsgBox "Table NOT Exists."

If a table is not exists then the 4th line show a message.

Table Exists

Sub...

  If ActiveDocument.Tables.Count = 0 Then
    MsgBox "Table NOT Exists."
  Else
    MsgBox "Table Exists!"

If a table is exists then the 6th line show a message.

Sub CheckIfTableExist_ActiveDocument()

  If ActiveDocument.Tables.Count = 0 Then
    MsgBox "Table NOT Exists."
  Else
    MsgBox "Table Exists!"
  End If

End Sub

Properties

http://www.relief.jp/itnote/archives/word-vba-determine-is-table.php

Apply To

  • Word 2013
  • Word 2010
  • Word 2007