We help IT Professionals succeed at work.

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

WonHop
WonHop asked
on
104 Views
Last Modified: 2019-02-19
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

CERTIFIED EXPERT
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
CERTIFIED EXPERT
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

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
CERTIFIED EXPERT
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.
CERTIFIED EXPERT
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
CERTIFIED EXPERT
Top Expert 2008
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

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.