[RESOLVED] VBA WORD -Multiple Tables in one doc
I am trying to make a template that will up to 7 different tables in it. I will need to refer to these tables individually and the only way i have found to do this is - Activedocument.tables(x). Problem is that not all the tables are there all the time so a table that was, say, number 6 one time may be number 4 another. Is there a way to use text names or locking a number to a particular table.
Thanks in advance.
Re: VBA WORD -Multiple Tables in one doc
Not sure but do the tables have a tag property? If so you can use that.
Re: VBA WORD -Multiple Tables in one doc
No, have not set up a tag. Will research that thank you....Any sample lines to get me started?
Re: VBA WORD -Multiple Tables in one doc
Take the syntax with a pinch of salt because I'm typing straight in rather than pasting from the IDE and I've never needed to manipulate tables in word but...
As you create the table set it's tag property to an incremental number, ie create the first table and set it's tag to '1', create the second and set it's tag to '2' and so on.
Code:
dim oTable as Table
set oTable = ActiveDocument.tables.add(whatever parms the add method needs)
oTable.Tag = 1
Then when you want to recover a particular table you can do this
Code:
dim oTable
dim theTableYouWant
For Each oTable in ActiveDocument.Tables
If oTable.tag = The table number you're looking for then
theTableYouWant = oTable
End if
Next Table
There's probably a more efficient way of searching through the tables collection than looping through checking each one like this that someone with more experience in this area can point you at. In particular go looking for a find method in the ActiveDocument.Tables collection.
Like I said though, take my syntax here with a LARGE pinch of salt and good luck
(RESOLVED) VBA WORD -Multiple Tables in one doc
Thanks fo help, Played about with tags but it did not work as needed. I have found a solution that worked for me. I post it here incase anyone else has similar problems. I assigned a bookmark to the whole table, that way i could use an If Exist to see if table was there.
Code:
Set tbl = Selection.Tables.Add(rng, iRows, iCols)
tbl.Select
Selection.Bookmarks.Add ("bkPmtTbl")
And to delete the table -
Code:
ActiveDocument.Bookmarks("bkPmtTbl").Select
Selection.Delete