|
楼主 |
发表于 2022-9-13 11:19
|
显示全部楼层
Sub 根据大纲选择标题及内容()
Dim n%, k%, i As Long, p As Long
Application.ScreenUpdating = False
With Selection.Find
.ClearFormatting
.text = "" '此处设置提取的名字,跟style可选择或组合使用
.Style = wdStyleHeading4 '此处设置选择大纲级别
.Forward = True
Do While .Execute
n = Selection.Range.Paragraphs.OutlineLevel
'判断起点
If Selection.Range.Paragraphs.OutlineLevel = n Then
起点 = Selection.Range.Start
Selection.GoTo wdGoToHeading, wdGoToNext, 1
Else
Do Until Selection.Range.Paragraphs.OutlineLevel = n
Selection.GoTo wdGoToHeading, wdGoToNext, 1
Loop
起点 = Selection.Range.Start
Selection.GoTo wdGoToHeading, wdGoToNext, 1
End If
'判断终点
If Selection.Range.Paragraphs.OutlineLevel = n Or Selection.Range.Paragraphs.OutlineLevel < n Then
终点 = Selection.Range.Start
'若提取的标题是文档最后一个
If 起点 = 终点 Then 终点 = ActiveDocument.Content.End: GoTo 最后一个标题
Selection.GoTo wdGoToHeading, wdGoToNext, 1
Else
Do Until Selection.Range.Paragraphs.OutlineLevel < n Or Selection.Range.Paragraphs.OutlineLevel = n
i = Selection.Range.Start
Selection.GoTo wdGoToHeading, wdGoToNext, 1
p = Selection.Range.Start
'判断光标不动,即到最后一个标题,跳出,设置终点
If i = p Then
终点 = ActiveDocument.Content.End
GoTo 最后一个标题
End If
Loop
终点 = Selection.Range.Start
Selection.GoTo wdGoToHeading, wdGoToNext, 1
End If
最后一个标题:
'选择
ActiveDocument.Range(起点, 终点).Select
Selection.Range.HighlightColorIndex = wdYellow '选择后处理
Selection.Collapse 0
Loop
End With
Application.ScreenUpdating = True
End Sub |
|