Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel VBA - How to convert my Word Table name string to a Word.Table object?

Status
Not open for further replies.

knifey

Technical User
Nov 14, 2006
180
GB
Hi,
I have a word table name string in the form of txtTableName:

Code:
For a1 = 1 To tblCount ' count of word tables
   txtTableName = "txtTable" & a1
   If Form1.Controls(txtTableName).Value <> "" Then
      'do stuff
   End If
Next a1

How can I convert this string to a Word.Table object so I can use this code from the ms website?

Code:
Sub PopulateAndExtendTable(ByRef tbl As Word.Table, ByRef aData() As Variant)
Dim nrRows As Long, nrCols As Long
For nrRows = 1 To UBound(aData, 1) + 1
    For nrCols = 1 To UBound(aData, 2) + 1
        tbl.Cell(nrRows, nrCols).Range.Text = aData(nrRows - 1, nrCols - 1)
    Next nrCols
    If nrRows <= UBound(aData, 1) Then
        tbl.Rows.Add
    End If
Next nrRows
End Sub

Any help of advice would be much appreciated
Thanks,
Roy
 
Something like this ?
Code:
Dim tbl As Word.Table
For a1 = 1 To tblCount ' count of word tables
   txtTableName = "txtTable" & a1
   If Form1.Controls(txtTableName).Value <> "" Then
      Set tbl = ActiveDocument.Tables(a1)
      'do stuff
   End If
Next a1

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PHV,
That looks like just what I'm after.
Roy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top