MS Word - Identify the Table Below Text That Is Found.

WonHop
WonHop used Ask the Experts™
on
This is question 2 of 3 that I am asking.   Microsoft Word 2016 VBA.    I am using fake data because I can't put company data out here.

I have code that finds the words that I need.  I will do a search and find "TABLE C-1"  
When it finds  "TABLE C-1", I need to identify the Table directly below the Text.  Once it is identified, I need to set focus/put the cursor in the first cell so that I can get all of the data from the table.  I have found code that will go thru the table can get the data.

Please see the attached file.

Thanks
WonHop
EE_Sample_01.docx
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2008

Commented:
Try the following:

Selection.Next(wdparagraph, 1).Select
Selection.Collapse

Open in new window

Author

Commented:
Hello. irudyk.  Thanks for your response.  How do I get the Table index number so that I can reference it and go to the first cell in the table?

BTW.  I have to leave for the day and I will be back on Tuesday to check for any other responses.  Have a great weekend.

Thanks
WonHop
Top Expert 2008

Commented:
The above code will put you in the first cell of the table.

If you then define your table variable as for example varTable = Selection.Tables(1), you can then parse each cell in the varTable.

I suppose if you really need to know it's index value you could set a variable to = ActiveDocument.Range(0, Selection.Tables(1).Range.End).Tables.Count
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Author

Commented:
Hello irudyk.  My question 2 and 3 are getting the same 5981 error message.  I have attached a photo of the error message.

This is the code I tried.
strTableIndex = ActiveDocument.Range(0, Selection.Tables(1).Range.End).Tables.Count

The thing is, there could be over 40 table in the document that is over 125 pages.  That is why I need to be able to identify that particular table so that I get the correct data from the correct table.
Error_Message.JPG
Top Expert 2008

Commented:
I see what you are saying but, the code I provided you puts you in the table you want to be in.

So if you set a variable when you are in the table as varTable = Selection.Tables(1) (I previously stated incorrectly that it was Selection.Table(1)) you can then parse varTable for each cell.

I'd have to look into the error you are getting, but if you use the above, it is a moot point.
Top Expert 2008

Commented:
The error is likely related to what’s happening in your 3rd question and is related to how your FindRange object is defined. Providing that information would be very useful.

Author

Commented:
Sorry for the delay.  I was off on Monday.

    Dim FindRange As Word.Range
    Set FindRange = ActiveDocument.Range
Top Expert 2008
Commented:
Try the following code:

    FindRange.Select
    Selection.Next(wdParagraph, 1).Select
    If Selection.Information(Word.WdInformation.wdWithInTable) = True Then
        Selection.Collapse
        'place your code to obtain information from table here
    End If

Open in new window

Author

Commented:
This does work.  I will have to add my code to it for the other part.  But you did answer the question that I asked here.

Thank you very much.
:0)
WonHop

Like I said the the other question, please stand by.  I am sure I will be back.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial