问题描述
我需要查找表格中带项目符号或编号的段落。如何在 MS Word 文档中使用 VBA 做到这一点?我试过遍历每个段落:
For Each DocPara In ActiveDocument.Paragraphs
Set rngPara = DocPara.Range
If rngPara.Tables.Count > 0 Then
Set lRange = rngPara.Tables.Item(1).Cell(1,1).Range
'How to test if cell contents has pargraph(s) that are bulleted or numbered?
End if
Next DocPara
解决方法
由于您只对表格中的项目符号或编号段落感兴趣,因此循环浏览文档中的所有段落效率很低。而是遍历所有表格并遍历每个表格中的段落。
Dim para As Paragraph
Dim tbl As Table
For Each tbl In ActiveDocument.Tables
For Each para In tbl.Range.Paragraphs
With para.Range
If Len(.ListFormat.ListString) > 0 Then
'para is part of a list
End If
End With
Next para
Next tbl
如果您必须处理文档的所有段落,则有一种更好的方法可以确定某个段落是否在表格中。
For Each DocPara In ActiveDocument.Paragraphs
With DocPara.Range
If .Information(wdWithInTable) Then
'para is in table
End if
End With
Next DocPara
,
For Each DocPara In ActiveDocument.Paragraphs
Set rngPara = DocPara.Range
If rngPara.Tables.Count > 0 Then
If Not rngPara.ListFormat.List Is Nothing Then
'is a list?
Debug.Print rngPara.ListFormat.ListString
End If
End If
Next DocPara