Link to home
Start Free TrialLog in
Avatar of Opera362
Opera362Flag for Spain

asked on

How to obtain a table ID (Word automation)

Hi all,

I have a bookmark inside a table in a Word document. I need to access directly from VBA to the cells of that table. I would like to do with:

ActiveDocument.Tables(tableID).Rows(rownumber).Cells(cellnumber).etc.

But, how can I know the "TableID" which a bookmark is in?
Actually, I've found to modify cells more or less with:
mywordobj.ActiveDocument.bookmarks("bkmID").Select
mywordobj.Selection.SelectRow
mywordobj.Selection.Cells(1).Range.Text = "<some text>"

But I don't want to do that way, because when the cell is updated, I lose the selection and I have to start again selecting the row, etc.

If you have any suggestions to access table cells in a different manner, they will be welcome :-)

Many thanks
ASKER CERTIFIED SOLUTION
Avatar of gilbar
gilbar

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 Opera362

ASKER

I want to answer my own question and help you also with that, gilbar, as you also said that were looking for that info.

I've finally found how to obtain some Table index, or some other object that has a Range property:

In brief, the trick is to, while staying at a  make a Range from the beginning of the document, to the actual range.end. Then do a Table.Count and you now have the table index nomber in the integer it returns ;-) I think it's a smart solution!!!

Sample code:
Dim itable As Integer
Dim myTable As table

' Goto bookmark, and select it
myWordObj.ActiveDocument.bookmarks("bkmID").Select

' Get index of table bookmark is inside of (GOOD!!!)
itable = myWordObj.ActiveDocument.Range(0, Selection.Tables(1).Range.End).Tables.Count

' Now capture that table
Set myTable = myWordObj.ActiveDocument.Tables(itable)
   
' And then, it's easy to do things like this
myTable.Cell(nrow, ncol).Range.Text = "At last!!"


You can view full explanation at:
"Determine the index number of the current paragraph, table, section ..."
http://www.mvps.org/word/FAQs/MacrosVBA/GetIndexNoOfPara.htm
I gave you the points gilbar for the time you spent writing that code, etc. Although it wasn't the solution I was looking for, it gave me an advice. Thanks for your time!
:-)
Avatar of gilbar
gilbar

Thank YOU opera362, your plan certainly does the trick