Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

ms word - How to access a table within a range nested in another table?

In order to access the single table within a range (say, rngOuter) I used: tblOuter = rngOuter.Tables[1]; After I placed a nested table within a range (say, rngInner) within that outer range's table, I found that: tblInner = rngInner.Tables[1]; did not work. rngInner.Tables[1] references tblOuter, rather than the table within itself. In fact, Tables collection of rngInner has only one element, and that is tblOuter. In order to access tblInner, I have to get at tblOuter.Range.Tables[1].

Does anyone know if I am making a mistake, or that's the way it is?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

AFAIK "that's the way it is", but you can look for cells that contain tables by using Cell.Tables rather than Cell.Range.Tables. e.g. to look for cells in the current selection that contain tables you could use

Sub listInnerTables()
Dim c As Cell
Dim r As Range
Dim t As Table
Dim tcount As Long
Set r = Selection.Range
If r.Tables.Count > 0 Then
  tcount = 0
  For Each t In r.Tables
    tcount = tcount + 1
    For Each c In t.Range.Cells
      If c.Range.InRange(r) Then
        If c.Tables.Count > 0 Then
          Debug.Print "Table: " & CStr(tcount) & _
            vbTab & " Row: " & CStr(c.RowIndex) & _
            vbTab & " Col: " & CStr(c.ColumnIndex) & _
            vbTab & " Table count: " & CStr(c.Tables.Count)
        End If
      End If
    Next
  Next
End If
Set r = Nothing
End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...