VBA: how to determine in which Table your Cursor is in a Word Document that has several tables

In a Word Document I have several tables. In my Macros I can chose to run some code on one or the other of these tables by referring to the index number of a table with a line like this
ActiveDocument.Tables(1)
or
ActiveDocoument.Tables(2)

I would like to determine in which Table my Cursor is by Running a Macro.
So basically if I run my Macro when my Cursor is in (any Cell of) the Second Table in my Word Document I would like to display a MessageBox showing “2” and if my Cursor is in (any Cell of) the Table 1 I would like to display a MessageBox showing “1” and likewise for any other tables in my Word Document.
LVL 1
FaheemAhmadGulAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
This will return the index number of the table that contains the whole of the Selection. It returns 0 if there is no such table
Sub WhichTable()
    Dim tbl As Table
    Dim t As Integer
    Dim ts As Integer
    For Each tbl In ActiveDocument.Tables
        t = t + 1
        If Selection.Range.InRange(tbl.Range) Then
            ts = t
            Exit For
        End If
    Next tbl
    MsgBox ts
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
FaheemAhmadGulAuthor Commented:
Excellent!
Thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.