Timothy Alston
Timothy Alston

Reputation: 1549

Counting the number of tables in a Word document

I have a Word document with many tables.

I need to count the number of tables.

I have tried the following VBA

Dim T as Table
Dim i as Integer
Dim Tables as Integer

For Each T In wdDoc.Tables
    i = i + 1
    Exit For
Next
Tables = i
End sub

However this comes back with

"Run-time error '424': Object required".

I also found on the internet the code

Tables = wdDoc.Tables.Count

Creating a macro with this code doesn't seem to do anything.

Upvotes: 4

Views: 15655

Answers (2)

Connor Campbell
Connor Campbell

Reputation: 61

Using Word 2016, the recommended answer was still running into an error. I tried a slightly revised version combining the answer & comments above. Having a period between "Active" and "Document" caused the macro to fail. I swapped in Message Box for Debug.Print, and this worked:

Sub CountTables()
'
' CountTables Macro
'
'
MsgBox ActiveDocument.Tables.Count
    
End Sub

Upvotes: 3

GijsA
GijsA

Reputation: 250

This should work:

   Debug.Print Active.Document.Tables.Count

Upvotes: 5

Related Questions