Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 301
  • Last Modified:

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
0
Opera362
Asked:
Opera362
  • 2
  • 2
1 Solution
 
gilbarCommented:
Opera;
try using a goto( which returns a range):

Set r1 = ActiveDocument.GoTo(what:=wdGoToBookmark, Name:="bkmID")
r1.InsertAfter "<some text>"
Selection.TypeText "woowoo!!"

the selection point stays where it was before, except now it says "woowoo!!", and you bookmarknow says "<some text>"

i have NO IDEA how to return the table id, and it's starting to really bother me.  hope someone else answers with that info

0
 
Opera362Author Commented:
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
0
 
Opera362Author Commented:
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!
:-)
0
 
gilbarCommented:
Thank YOU opera362, your plan certainly does the trick
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now