vba - 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。
谢谢你。
解决方案
您可以通过获取表格计数来测试段落范围是否在表格中:如果它大于零 ( Range.Tables.Count > 0
),则该范围在表格中。还有较旧的 WordBasic 方法:Range.Information(wdWIthinTable) = true
.
要确定范围是否是列表的一部分(无论是项目符号还是编号),您可以使用Range.ListFormat.ListType
. 这将返回WdListType
枚举的成员。wdListNoNumbering
是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
推荐阅读
- notifications - Flutter:如何仅当我们使用 local_notification 点击通知时才执行操作?
- javascript - 把双引号换成单引号?
- angularjs - 使用复选框编辑表中的单个或所有选定用户 - AngularJS
- javascript - 在javascript中为表格保留空间
- c# - @Html.DropDownListFor 的数据绑定异常
- java - 没有 3rd 方库的 Java 屏幕抓取
- javascript - 与 Javascript for Visual Studio 2017 相关的语法高亮颜色设置列表
- intellij-idea - IntelliJ 工件构建未从终端运行
- css - FontAwesome 为什么图标以 SVG 格式呈现?
- webpack - webpack-serve 不提供捆绑文件 (404s)