Link to home
Start Free TrialLog in
Avatar of WonHop
WonHopFlag for United States of America

asked on

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

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
Avatar of irudyk
irudyk
Flag of Canada image

Try the following:

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

Open in new window

Avatar of WonHop

ASKER

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
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
Avatar of WonHop

ASKER

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
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.
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.
Avatar of WonHop

ASKER

Sorry for the delay.  I was off on Monday.

    Dim FindRange As Word.Range
    Set FindRange = ActiveDocument.Range
ASKER CERTIFIED SOLUTION
Avatar of irudyk
irudyk
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WonHop

ASKER

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.