Link to home
Start Free TrialLog in
Avatar of ewingdj
ewingdjFlag for United States of America

asked on

How to reference a table in Word other than by index number.

I am creating an MS Word document through automation from  an MS Access form. In this Word document I insert multiple .dot templates containing predefined tables using the following code:

wordobj.Selection.InsertFile FileName := file name ....

My question is, since the number of tables (.dot templates) that get inserted varies depending on existing data , how do I reference each table other than by its index number. I need to be able to bounce back and forth between tables. Currently I'm using the following code to reference a particular cell within a table:

wordobj.ActiveDocument.Tables(9).Cell(x,y).Select
wordobj.Selection.Collapse

However, this hard-coded index reference assumes that all previous tables have been inserted which may not always be the case.

Thanks in advance,
Don
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

The question is a bit open but if inserting tables for example at the end then the number of tables can be used and incremented i.e. first inserted table is number:

wordobj.ActiveDocument.Tables.count + 1

If necessary something similar can be done for any insertion point if you can give more info on the process

Chris
You could select each table in turn, and insert a bookmark for that table, giving it an appropriate name.  Then you could use the bookmark names to go to the desired table.
The bookmarks could be created immediately after inserting each table into the document.
ASKER CERTIFIED SOLUTION
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland 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 ewingdj

ASKER

Chris,

I thought about setting up a variable to keep count of the tables as they are inserted into the document and then use this variable as the table index. I was just curious if there was some other means to identify a table other than by its index number.

In my document, certain information stored in one table needs to be added to previous tables, Therefore, I need to be able to reference each table/cell individually since the actual number of inserted tables is data-dependent.
Avatar of ewingdj

ASKER

Thanks for the quick response.