?
Solved

How to obtain a table ID (Word automation)

Posted on 2003-02-27
4
Medium Priority
?
262 Views
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:

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
Comment
Question by:Opera362
[X]
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
4 Comments
 
LVL 9

Accepted Solution

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

Author Comment

by:Opera362
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
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
 
LVL 3

Author Comment

by:Opera362
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!
:-)
0
 
LVL 9

Expert Comment

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

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