如何循环遍历具有拆分单元的字表中的所有单元格

Vin*_*nod 7 iteration split ms-word cells word-vba

我在MS word中有一个3 X 3(比如tableA)表.第(2,2)个细胞是分裂细胞(分成2X2表).如何循环遍历表格A中的所有单元格.

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 Code Online (Sandbox Code Playgroud)

该程序给出了编译错误:

Run time error 5914
The requested member of the collection does not exist
Run Code Online (Sandbox Code Playgroud)

如何迭代具有拆分单元格的表格的单元格.

Kaz*_*wor 5

您应该假设每行都有最大可能的列数.在你的情况下,那将是四个.为了迭代每个单元格,我建议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
Run Code Online (Sandbox Code Playgroud)

  • 我的解决方案很久以前就已经提供了.但回答在activedocument中第一个表格中循环每个单元格的问题,我建议使用以下代码:`For Each C In ActiveDocument.Tables(1).Range.Cells` >>`'在这里做你需要的每个单元格>> >> `下一个C` (2认同)