VBA-选择标题的内容

发布时间 2023-05-02 15:16:58作者: 狂自私

简单说明

这个是模仿但是不是wod自带的功能:选择标题和内容

 这个功能能方便的快速选择这个标题下面的所有内容。

要选定是因为我要对这个标题下面的子标题进行排序,但是排序的话,不能有父标题,也就是说,选择的内容中的最高标题要是同级别(有父标题就排序父标题去了,但是父标题又只有一个,排序个der啊)。

vba代码

 1 Function select_range(start_title_str As String, end_title_str As String, Optional style_str As String = "标题 1") As Boolean
 2     '选择范围,通过指定本标题字符串(start_title_str)和下一个标题字符串(end_title_str),选择它们之间的内容
 3     '若是end_title_str为空,则认为从start_title_str开始选择到当前节的末尾
 4     'style_str是可选参数,它有一个默认值,该值用于查询的时候指定标题样式名
 5     '若是start_title_str为空,则不进行任何操作
 6     '本函数成功选择则返回true,失败返回false
 7     If (Len(start_title_str) <> 0) Then
 8         Selection.Find.ClearFormatting
 9         Selection.Find.Style = ActiveDocument.Styles(style_str)
10         With Selection.Find
11             .Text = start_title_str
12             .Forward = True
13             .Wrap = wdFindContinue
14             .Format = True
15             .MatchCase = False          '是否区分大小写
16             .MatchWholeWord = False
17             .MatchByte = True
18             .MatchWildcards = False
19             .MatchSoundsLike = False
20             .MatchAllWordForms = False
21             .MatchWholeWord = True      '是否全字匹配
22             .Execute
23         End With
24         If (Selection.Find.Found) Then
25             range_start_index = Selection.Start + Len(Selection.Find.Text) + 1
26             If (Len(end_title_str) <> 0) Then
27                 Selection.Find.Text = end_title_str
28                 Selection.Find.ClearFormatting
29                 Selection.Find.Style = ActiveDocument.Styles(style_str)
30                 With Selection.Find
31                     .Forward = True
32                     .Wrap = wdFindContinue
33                     .Format = True
34                     .MatchCase = False          '是否区分大小写
35                     .MatchWholeWord = False
36                     .MatchByte = True
37                     .MatchWildcards = False
38                     .MatchSoundsLike = False
39                     .MatchAllWordForms = False
40                     .MatchWholeWord = True      '是否全字匹配
41                     .Execute
42                 End With
43                 If (Selection.Find.Found) Then
44                     range_end_index = Selection.Start
45                     Selection.Start = range_start_index '设定选择的开始位置
46                     Selection.End = range_end_index     '设定选择的结束位置
47                     select_range = True
48                 Else
49                     MsgBox ("未查询到 " + end_title_str)
50                     select_range = False
51                 End If
52             Else
53                 Selection.Start = range_start_index '设定选择的开始位置
54                 Selection.Expand (wdSection)        '拓展选择到当前所在节的末尾
55                 select_range = True
56             End If
57         Else
58             MsgBox ("未查询到 " + start_title_str)
59             select_range = False
60         End If
61     Else
62         select_range = False
63     End If
64 End Function
View Code

相关用途

在指定的范围内查询

 1 Sub cmdlet_命令查询()
 2 '
 3 ' 查找 宏
 4 '
 5 '
 6     Result = select_range("cmdlet 命令", "Server 2016 core")
 7     Selection.Find.ClearFormatting
 8     Selection.Find.Style = ActiveDocument.Styles("标题 2")
 9     With Selection.Find
10         '.Text = "Exit-PSSession"
11         .Text = InputBox("请输入要查找的命令名称:", "cmdlet 命令查询")
12         '.Replacement.Text = ""     '不进行替换
13         .Forward = True
14         .Wrap = wdFindContinue
15         .Format = True
16         .MatchCase = False          '是否区分大小写
17         .MatchWholeWord = False
18         .MatchByte = True
19         .MatchWildcards = False
20         .MatchSoundsLike = False
21         .MatchAllWordForms = False
22         .MatchWholeWord = True      '是否全字匹配
23     End With
24     If Not (Selection.Find.Execute) Then         '执行查找
25         Result = MsgBox("没有查询到该命令,请键入该命令的正式名称而非别名、简称;若还是没有,请新增该命令", 0, "未查询到")
26     End If
27 End Sub
View Code

调整标题的顺序

 1 Sub 调整about标题的顺序()
 2 '
 3 ' 调整about标题的顺序 宏
 4 '
 5 '
 6     Result = select_range("about配置文件", "实战:远程巡检希沃一体机的运行信息")
 7     If (Result) Then
 8         Selection.SortByHeadings wdSortFieldSyllable, wdSortOrderAscending
 9     Else
10         MsgBox ("选定失败,无法排序。")
11     End If
12 End Sub
View Code