2013-04-23 158 views
1

我正在扫描包含表格内部和外部的列表元素的文档。一旦我找到一个列表元素,我将它的层次结构编号和与它相关的文本导出到Excel中。我的扫描是基于列表元素而不是专门的表格。确定哪个表ListParagraph元素出现在Word VBA中

在某些情况下,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 

回答

0

这里没有对象Columns。您可以使用

...Rows(oCurrentRow).Cells.Count 

获取行中单元格的数量。或者,更好的是,您可以通过For Each来循环访问单元格,而不需要对它们进行计数。

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 
+0

谢谢,我在前一段时间发现了自己愚蠢的错误,但由于我是新手,无法发布它。 我可以使用上面提出的方法,但它仍然需要额外的功能,因为如上所述,我使用计数器向表格中的每列右移。 虽然再次感谢! – Eagles5iveBC 2013-04-23 19:35:11