Get table count in MS Word document

User avatar
Sam1085
3StarLounger
Posts: 318
Joined: 23 Aug 2016, 07:43
Location: Sri Lanka

Get table count in MS Word document

Post by Sam1085 »

Hi,

I've a macros to find table by table in word document. Please find below sample code:

Code: Select all

Sub Find_Tables()
Dim n As Long
nCount = ActiveDocument.Tables.Count
If nCount = 0 Then
  MsgBox "Table Count = 0", vbOKOnly, Title
    Exit Sub
Else
End If

    Application.Browser.Target = wdBrowseTable
    Application.Browser.Next
    Selection.Tables(1).Select
End Sub
Above macro works perfectly for my documents. I've to develop this macro to count number of tables if I navigate to the last table or end of the document.

Any help would be greatly appreciate.
Thanks!
-Sampath-

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Get table count in MS Word document

Post by HansV »

ActiveDocument.Tables.Count returns the number of tables in the document.
Best wishes,
Hans

User avatar
Sam1085
3StarLounger
Posts: 318
Joined: 23 Aug 2016, 07:43
Location: Sri Lanka

Re: Get table count in MS Word document

Post by Sam1085 »

Hi Hans,

Is there any way to run 'ActiveDocument.Tables.Count' only when cursor place on last table of the doc or end of the doc.
-Sampath-

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Get table count in MS Word document

Post by HansV »

For example:

Code: Select all

Sub Find_Tables()
    Dim n As Long
    n = ActiveDocument.Tables.Count
    If n = 0 Then
        MsgBox "Table Count = 0", vbOKOnly
        Exit Sub
    Else
        Application.Browser.Target = wdBrowseTable
        Application.Browser.Next
        Selection.Tables(1).Select
        If ActiveDocument.Range(0, Selection.End).Tables.Count = n Then
            MsgBox "Table Count = " & n, vbOKOnly
        End If
    End If
End Sub
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Get table count in MS Word document

Post by HansV »

By the way, you declared a variable n but didn't use it, and you used a variable nCount without declaring it.

Please take a look at The importance of 'Option Explicit'.
Best wishes,
Hans

User avatar
Sam1085
3StarLounger
Posts: 318
Joined: 23 Aug 2016, 07:43
Location: Sri Lanka

Re: Get table count in MS Word document

Post by Sam1085 »

Thank you Hans,

It's works as I needed.

One more thing Hans,

I received run time error when go to the end of the document (Not inside of the table) and run your macro. How to develop this macro to get number of table count, if cursor not place on a table?

Thanks again!
-Sampath-

User avatar
Sam1085
3StarLounger
Posts: 318
Joined: 23 Aug 2016, 07:43
Location: Sri Lanka

Re: Get table count in MS Word document

Post by Sam1085 »

Thank you Hans,

Very useful forum thread for me.
-Sampath-

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Get table count in MS Word document

Post by HansV »

Does this work better?

Code: Select all

Sub Find_Tables()
    Dim n As Long
    n = ActiveDocument.Tables.Count
    If n = 0 Then
        MsgBox "Table Count = 0", vbOKOnly
        Exit Sub
    Else
        Application.Browser.Target = wdBrowseTable
        Application.Browser.Next
        If Selection.Information(wdWithInTable) Then
            Selection.Tables(1).Select
        End If
        If ActiveDocument.Range(0, Selection.End).Tables.Count = n Then
            MsgBox "Table Count = " & n, vbOKOnly
        End If
    End If
End Sub
Best wishes,
Hans

User avatar
Sam1085
3StarLounger
Posts: 318
Joined: 23 Aug 2016, 07:43
Location: Sri Lanka

Re: Get table count in MS Word document

Post by Sam1085 »

This is exactly what I needed. Thanks lot!! :thankyou:
-Sampath-

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Get table count in MS Word document

Post by HansV »

Best wishes,
Hans

User avatar
Sam1085
3StarLounger
Posts: 318
Joined: 23 Aug 2016, 07:43
Location: Sri Lanka

Re: Get table count in MS Word document

Post by Sam1085 »

Thank you Hans for sharing this vital information,

Clearly explained everything behind the VBA with examples. I'm not familiar with VBA, But this articles will be very useful to build basic knowledge about VBA coding.

Appreciate if you could write an article for VBA vs VSTO advantages and disadvantages.

Thanks!
-Sampath-

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Get table count in MS Word document

Post by HansV »

I'm not an expert in VSTO, but here are a few points:

VBA:
  • Native programming language of Microsoft Office: code can be created, edited and run within Word, Excel etc.
  • Code can be stored in Office documents, so each file can contain its own code.
  • Interpreted, so relatively slow (but in most situations, that is not a problem).
VSTO:
  • Can be used in Visual Studio to create stand-alone applications (.exe) and COM add-ins.
  • Can not be created or stored in Office documents.
  • Compiled code, so runs fast.
Best wishes,
Hans

User avatar
Sam1085
3StarLounger
Posts: 318
Joined: 23 Aug 2016, 07:43
Location: Sri Lanka

Re: Get table count in MS Word document

Post by Sam1085 »

Thank you Hans for the info.

This article also helpful to identify few basic point and coding structures (C# and Visual Basic): https://msdn.microsoft.com/en-us/library/ee342218.aspx" onclick="window.open(this.href);return false;
-Sampath-

User avatar
Sam1085
3StarLounger
Posts: 318
Joined: 23 Aug 2016, 07:43
Location: Sri Lanka

Re: Get table count in MS Word document

Post by Sam1085 »

HansV wrote:Does this work better?

Code: Select all

Sub Find_Tables()
    Dim n As Long
    n = ActiveDocument.Tables.Count
    If n = 0 Then
        MsgBox "Table Count = 0", vbOKOnly
        Exit Sub
    Else
        Application.Browser.Target = wdBrowseTable
        Application.Browser.Next
        If Selection.Information(wdWithInTable) Then
            Selection.Tables(1).Select
        End If
        If ActiveDocument.Range(0, Selection.End).Tables.Count = n Then
            MsgBox "Table Count = " & n, vbOKOnly
        End If
    End If
End Sub

Hi Hans,

May I know how to get the number of table count in Word document to new excel workbook.

Ex:
Imag 1.png
I have no idea about how to control excel by Word VBA.

Thank you!
You do not have the required permissions to view the files attached to this post.
-Sampath-

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Get table count in MS Word document

Post by Rudi »

Try this...

Code: Select all

Sub TableCountToExcel()
Dim xl As Object
Dim wb As Object
Dim sh As Object
Dim rg As Object
Dim sName As String

    On Error Resume Next
    Set xl = GetObject(, "Excel.Application")
    If xl Is Nothing Then
        Set xl = CreateObject("Excel.Application")
        xl.Visible = True
    End If
    sName = "C:\Users\rudis\Desktop\SampleFile.xlsx" '< Change Path
    Set wb = xl.workbooks.Open(sName)
    If wb Is Nothing Then Set wb = xl.workbooks.Add
    On Error GoTo 0
    Set sh = wb.sheets.Add 'or use specific sheet: wb.sheets(1)
    Set rg = sh.Range("A1")

    With xl
        rg.Value = "No. of tables in Word Document:"
        rg.Offset(0, 1).Value = ActiveDocument.Tables.Count
        sh.Columns(1).ColumnWidth = 30
    End With
    
    Set rg = Nothing
    Set sh = Nothing
    Set wb = Nothing
    Set xl = Nothing
End Sub
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Get table count in MS Word document

Post by Rudi »

Note: I made a small edit in the code I posted above.
If you already copied it to try...just recopy again!
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
Sam1085
3StarLounger
Posts: 318
Joined: 23 Aug 2016, 07:43
Location: Sri Lanka

Re: Get table count in MS Word document

Post by Sam1085 »

Thank you Rudi,

It's very helpful for me. Now I can customize your code to count tables, paragraphs...etc.
-Sampath-

User avatar
Sam1085
3StarLounger
Posts: 318
Joined: 23 Aug 2016, 07:43
Location: Sri Lanka

Re: Get table count in MS Word document

Post by Sam1085 »

Hi Rudi,

May I know why set "Set rg = Nothing, Set sh = Nothing, Set wb = Nothing and Set xl = Nothing" at the end of the code?
-Sampath-

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Get table count in MS Word document

Post by Rudi »

Set obj = Nothing is a standard statement used to clear object variables and basically cleans up or releases it from memory. It was good practice to use on older systems, but its a bit archaic on modern systems today and it does not really matter if you don't use it; although more pedantic coders might argue with that!

Added:
In this macro it is not necessary as the End Sub statement would release all the variables and clean up. However, if this macro was part of a larger macro or was being called, its probably a good idea to ensure that these variables are being release to optimize your code more. Remember, if you do use these statements, your variable is destroyed and you cannot use it until it is (re)set again.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
Sam1085
3StarLounger
Posts: 318
Joined: 23 Aug 2016, 07:43
Location: Sri Lanka

Re: Get table count in MS Word document

Post by Sam1085 »

Thank you Rudi for explanation and your help!

Have a nice day.
-Sampath-