确定ListParagraph元素在Word VBA中显示的表

时间:2013-04-23 13:34:29

标签: vba ms-word word-vba

我正在扫描包含表格内外列表元素的word文档。找到列表元素后,我将其层次结构编号和与之关联的文本导出到Excel。我的扫描基于列表元素,而不是具体的表格。

在某些情况下,list元素将出现在表的第一列中,与其关联的文本将出现在该表的任意数量的后续列中。在某些表中有合并的单元格,因此我需要确定表格当前行中的列数。

我一直收到运行时错误'438':对象不支持tCol = oLI.Range.Tables(1).Rows(oCurrentRow).Columns.Count行上的此属性或方法,我不确定原因。

Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlWB = xlApp.Workbooks.Add ' create a new workbook
With xlWB.Worksheets(1)
    For Each oList In ActiveDocument.Lists
        For Each oLI In oList.ListParagraphs
            .Cells(i, 1) = oLI.Range.ListFormat.ListString
            If oLI.Range.Information(wdWithInTable) = True Then
                '#DETERMINE WHICH TABLE TO LOOK IN
                oCurrentRow = oLI.Range.Cells(1).RowIndex
                    'Determine which Row in the Table to look in
                tCol = oLI.Range.Tables(1).Rows(oCurrentRow).Columns.Count
                    'Determine how many Columns the Table contains
                Debug.Print tCol
                For j = 2 To tCol
                    .Cells(i, j) = oLI.Range.Tables(1).Cell(oCurrentRow, j)
                Next j
            Else
                .Cells(i, 2) = oLI.Range.Text
            End If
            i = i + 1
        Next oLI
    Next oList
End With

1 个答案:

答案 0 :(得分:0)

Columns对象没有Row。你可以使用

...Rows(oCurrentRow).Cells.Count

获取行中的单元格数。或者,更好的是,您可以使用For Each循环遍历Cells,而不必费心计算它们。

Sub LoopCells()

    Dim tbl As Table
    Dim tCell As Cell

    Set tbl = ThisDocument.Tables(1)

    For Each tCell In tbl.Rows(1).Cells
        Debug.Print tCell.Range.Text
    Next tCell

End Sub