How to obtain a table ID (Word automation)

Posted on 2003-02-27
Medium Priority
Last Modified: 2008-02-01
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:


But, how can I know the "TableID" which a bookmark is in?
Actually, I've found to modify cells more or less with:
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
Question by:Opera362
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Accepted Solution

gilbar earned 100 total points
ID: 8036050
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


Author Comment

ID: 8041096
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

' 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 ..."

Author Comment

ID: 8041147
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!

Expert Comment

ID: 8041551
Thank YOU opera362, your plan certainly does the trick

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a serious pitfall that can happen when deleting shapes using VBA.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question