Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting the number of tables in a Word document

Tags:

ms-word

vba

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.

like image 923
Timothy Alston Avatar asked Oct 26 '25 16:10

Timothy Alston


2 Answers

This should work:

   Debug.Print Active.Document.Tables.Count
like image 164
GijsA Avatar answered Oct 29 '25 17:10

GijsA


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
like image 20
Connor Campbell Avatar answered Oct 29 '25 16:10

Connor Campbell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!