VBA WORD 识别段落是列表还是表格

Ada*_*cho 3 vba ms-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
Run Code Online (Sandbox Code Playgroud)

所以,我需要知道当前段落是否是列表和表格。

谢谢。

Cin*_*ter 5

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

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

我更改了检查列表和表格的顺序,将表格放在第一位,前提是您首先需要了解这一点。(这样就不会检查列表了。)

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