10

MS Word で 3 X 3 (たとえば tableA) のテーブルがあります。(2,2) 番目のセルは分割セル (2X2 テーブルに分割) です。tableA のすべてのセルを循環するにはどうすればよいですか。

Dim strCellText As String
Dim uResp As String
Dim Row As Integer
Dim Col As Integer

Dim itable As Table


For Each itable In ThisDocument.Tables

    uResp = ""

    For Row = 1 To itable.Rows.Count

        For Col = 1 To itable.Columns.Count

            strCellText = itable.Cell(Row, Col).Range.Text
            uResp = uResp & Trim(strCellText)                

        Next

    Next

    MsgBox uResp
Next

このプログラムでは、コンパイル エラーが発生します。

Run time error 5914
The requested member of the collection does not exist

セルが分割されているテーブルのセルを反復するにはどうすればよいですか。

4

3 に答える 3

7

各行には可能な最大数の列があると想定する必要があります。あなたの状況では、それは 4 になります。各セルを反復するOn Error Resume Nextには、最初のループが開始する前に設定することを提案します。次に、内側のループ内で、次のコードを試してください。

strCellText = itable.cell(Row, Col).Range.Text

If Err.Number = 0 Then
    uResp = uResp & Trim(strCellText)
    Debug.Print Row, Col, strCellText
Else
    Err.Clear
End If
于 2013-03-23T10:45:10.253 に答える
2

(場合によっては不正な形式の) テーブルからデータを抽出しようとすると、この状況に遭遇します。これが私がそれを処理する方法です:

列数を確認する

For each row in table.rows
  if row.cells.count < expectedRows
    'You know you are lacking rows
  else
    'Normal processing
  end if
Next

とにかくすべてのデータが必要な場合は、各セルを調べます

For each row in table.rows
  For each cell in row.cells
    'Process individual cells
  Next
Next

セルが垂直に結合された場合、これらはどれも機能しません。

于 2015-05-20T11:12:54.067 に答える