首页 > 解决方案 > VBA WORD 识别段落是列表还是表格

问题描述

我有以下代码:

For Each DocPara In ActiveDocument.Paragraphs
    If (DocPara.style = "Title 1") Then
        ...
    Else

        (if DocPara is LIST then)
            ...
        (else if DocPara is TABLE then)
            ...
    End If
Next DocPara

所以,我需要知道当前段落是否是 LIST 和 TABLE。

谢谢你。

标签: vbams-word

解决方案


您可以通过获取表格计数来测试段落范围是否在表格中:如果它大于零 ( Range.Tables.Count > 0),则该范围在表格中。还有较旧的 WordBasic 方法:Range.Information(wdWIthinTable) = true.

要确定范围是否是列表的一部分(无论是项目符号还是编号),您可以使用Range.ListFormat.ListType. 这将返回WdListType枚举的成员。wdListNoNumbering0- 您可以使用任一值。如果该信息有用,枚举的其他成员可以告诉您它是项目符号(以及什么类型)或数字(什么类型的列表)。

我已经更改了检查列表和表格的顺序,将表格放在首位,前提是您首先需要知道这一点。(检查列表将不会发生。)

Sub CheckParaType()
    Dim DocPara As Word.Paragraph
    Dim rngPara As Word.Range       

  For Each DocPara In ActiveDocument.paragraphs
    Set rngPara = DocPara.Range
    If (DocPara.style = "Title") Then
        Debug.Print "Style is OK"
    ElseIf rngPara.Tables.Count > 0 Then
        Debug.Print "It's in a table"
    ElseIf rngPara.ListFormat.ListType <> 0 Then
        Debug.Print "It's a list."
    Else
        Debug.Print "the paragraph is something else"
    End If
  Next DocPara
End Sub

推荐阅读