PDA

View Full Version : Find Table number with bookmark



Jacko_B
07-10-2009, 07:54 PM
I have an array of data that I wish to put in a Table in a Word document, which contains several Tables. I will be using VBA

To locate the starting point of the data column in the table, I have inserted a bookmark.

Does anyone know how I can then determine the Table number from the BookMark properties using VBA?

regards,
Jacko

macropod
07-11-2009, 04:02 AM
Hi Jacko,

If you've bookmarked the table, you really don't need to know which table in the document it is - you can work with the table directly using code like:

Sub Demo()
Dim oCel As Cell, i As Integer
With ActiveDocument.Bookmarks("MyTable").Range.Tables(1).Range
For Each oCel In .Cells
oCel.Range.Text = i
i = i + 1
Next
End With
End SubFor the above, the table's bookmark name is 'MyTable'.

JillB
07-11-2009, 03:15 PM
Thanks for this, macropod, much appreciated.

I am a newbie when it comes to Word VBA ( but not Excel).

The Word document I am using, prepared as a template by others, has many tables ( some used for normal text ), so it is difficult to know which Table number a table is eg Table(n) where I am looking for n

I need the value of n so as to know where to start transferring data into a specific table using VBA. In fact, if I also had the column number and the cell number, that would pinpoint it exactly.

Can anyone help here please?

macropod
07-11-2009, 10:28 PM
Hi Jill (Jacko?),

In your(?) original post, it was said "To locate the starting point of the data column in the table, I have inserted a bookmark". That being the case, the code I provided shows how to work with the bookmarked table - you really don't need to know what the table # it might be to do that.

If the bookmark is in a particular cell within the table, you can locate the bookmarked cell with code like:
Sub Demo()
Dim BkMk As String
BkMk = "MyTable"
With ActiveDocument.Bookmarks(BkMk).Range.Tables(1).Range
With .Bookmarks(BkMk).Range.Cells(1)
MsgBox "Column: " & .ColumnIndex & ", Row: " & .RowIndex
End With
End With
End Sub

Jacko_B
07-11-2009, 11:01 PM
Thanks macropod. I actually signed up with this forum the other day with my name Jacko , then today when searching the forum that I found an item under my sister's name. She had signed up some 12 months ago , but only used it twice.

Today I logged in under Jill's name to see if it still worked, and while there answered this post in her name. I realised my mistake straight away, but could not delete the post.



Thanks for the code, much appreciated. I am on a real steep learning curve.

regards

Jacko ( me)

fumei
07-13-2009, 12:11 PM
"so it is difficult to know which Table number a table is eg Table(n) where I am looking for n"

Indeed it is. With Table(n), n always means, and ONLY means, the order in the document.

Table(3) means the third table in the document.
If you put another table in before it, Table(3) automatically becomes Table(4).

For this reason, Table(index) has limited use. It is MUCH better if you have tables bookmarked. You can then action that particular table by name, no matter where it is, where it has possibly been moved, or even if it has changed the number of rows/columns.