Finding the first table after a string

B

bzamfir

Hi,

I have a document with several tables in it, and before each two tables I
have a title string, which is unique.

I need to update some data in table cells, which I can do using Cells
collection for the table object.

However, I don't know how can I find a reference to those two tables who
follows the title.

I don't want to use document.tables(idx), since it might be possible to
change tables order sometimes.

How can I do this?

Best regards,
Bogdan
 
B

bzamfir

Just replaying to myself. I post the code, just in case it might be useful
to others.
Any comment or improvement are welcome.


Dim myRange As Range
With ActiveDocument.Content.Find
.Text = "My title"
.Forward = True
.Execute
If Not .Found Then
MsgBox "Cannot find the specific title"
Exit Sub
End If
Set myRange = ActiveDocument.Range(Start:=.Parent.Start,
End:=ActiveDocument.Range.End)
End With

Dim tbl As Table
Set tbl = myRange.Tables(1) ' reference to first table after the
title

PrintTable tbl

Set tbl = myRange.Tables(2) ' reference to first table after the
title

PrintTable tbl
.................


sub PrintTable(byval tbl as Table)

Dim i As Integer, j As Integer
Dim rng As Range
For i = 1 To tbl.Rows.Count
For j = 1 To tbl.Columns.Count
Set rng = tbl.Cell(i, j).Range
Debug.Print "Cell [" & i & "," & j & "] " & rng.Text
Next
Next

end sub


Regards,
Bogdan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top