ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 4806|回复: 12

[分享] 多级标题自动编号(宏)

[复制链接]

TA的精华主题

TA的得分主题

发表于 2019-8-18 22:27 | 显示全部楼层 |阅读模式
本帖最后由 413191246se 于 2019-8-18 22:48 编辑

* 不喜欢折腾样式的朋友们,可以用本宏来给多级标题自动编号,标题级别分 5 级(标题 1~5)。
* 第1级标题只须打“第一章(或第1章);2/3/4/5级标题,只须打“1.1/1.1.1/1.1.1.1/1.1.1.1.1”。
* 请永远不必打“第二章/第三章”,不需要,只须打“第一章 XX”,“第1章 YY”,本宏会自动编号。
* 第1级标题(第X章)默认左对齐,有参数可以调整为居中;可以“第一章”,也可以“第1章”。
* 各级标题均采用 Word 内置样式(Word2003),格式可根据自己需要自行调整,谢谢大家!
  1. Sub 多级标题自动编号()

  2. '''初始化

  3.     With ActiveDocument
  4.         '通用模板内置样式复制到活动文档
  5.         .CopyStylesFromTemplate Template:=.AttachedTemplate.FullName

  6.         '删除所有域
  7.         .Fields.Unlink

  8.         '列表编号/LISTNUM域转文本
  9.         .ConvertNumbersToText

  10.         '回车符/手动换行符=>段落标记
  11.         .Content.Find.Execute "[^13^11]", , , 1, , , , , , "^p", 2

  12.         .Select

  13.         '清除格式/删除段落首尾空格
  14.         With Selection
  15.             .ClearFormatting
  16.             CommandBars.FindControl(ID:=122).Execute
  17.             CommandBars.FindControl(ID:=123).Execute

  18.             '删除所有空格
  19.             With .Find
  20.                 .ClearFormatting
  21.                 .Replacement.ClearFormatting
  22.                 .Execute "^w", , , 0, , , , , , "", 2
  23.                 .Execute " ", , , 0, , , , , , "", 2
  24.             End With

  25.             '正文样式
  26.             With .Font
  27.                 .Size = 14
  28.                 .Color = wdColorBlue
  29.             End With

  30.             .ParagraphFormat.CharacterUnitFirstLineIndent = 2
  31.         End With
  32.     End With

  33. '''
  34.     Const s As String = "一二三四五六七八九十百零〇○OoOo12345678901234567890"

  35.     Dim a&, b&, c&, d&, e&, j&, k&, n&, i As Paragraph

  36.     'j=0第一章/j=1=第1章
  37.     j = 0

  38.     'k=0=左对齐/k=1=居中(标题1)
  39.     k = 0

  40. '''Style

  41.     With ActiveDocument
  42.         For Each i In .Paragraphs
  43.             With i.Range
  44.                 If Not .Information(12) Then
  45.                     n = 1
  46.                     If .Text Like "第*" Then n = 2
  47.                     Do While InStr(s, .Characters(n)) > 0
  48.                         n = n + 1
  49.                         If .Characters(n).Text = "章" Then .Style = wdStyleHeading1: Exit Do
  50.                         If .Characters(n) Like "[!0-9]" Then
  51.                             If .Text Like "*.*.*.*.*" Then
  52.                                 .Style = wdStyleHeading5
  53.                             ElseIf .Text Like "*.*.*.*" Then
  54.                                 .Style = wdStyleHeading4
  55.                             ElseIf .Text Like "*.*.*" Then
  56.                                 .Style = wdStyleHeading3
  57.                             ElseIf .Text Like "*.*" Then
  58.                                 .Style = wdStyleHeading2
  59.                             End If
  60.                         End If
  61.                     Loop
  62.                 End If
  63.             End With
  64.         Next

  65. '''Color/Indent

  66.         With .Styles(wdStyleHeading1)
  67.             .Font.Color = wdColorRed
  68.             With .ParagraphFormat
  69.                 .SpaceBefore = 30
  70.                 .SpaceAfter = 24
  71.                 If k = 1 Then .Alignment = wdAlignParagraphCenter
  72.             End With
  73.         End With

  74.         With .Styles(wdStyleHeading2)
  75.             .Font.Color = wdColorPink
  76.             .ParagraphFormat.CharacterUnitFirstLineIndent = 1.75
  77.         End With

  78.         With .Styles(wdStyleHeading3)
  79.             .Font.Color = wdColorGreen
  80.             .ParagraphFormat.CharacterUnitFirstLineIndent = 1.75
  81.         End With

  82.         With .Styles(wdStyleHeading4)
  83.             .Font.Color = wdColorBrown
  84.             .ParagraphFormat.CharacterUnitFirstLineIndent = 2
  85.         End With

  86.         With .Styles(wdStyleHeading5)
  87.             .Font.Color = wdColorOrange
  88.             .ParagraphFormat.CharacterUnitFirstLineIndent = 2
  89.         End With

  90. '''AutoNum

  91.         .Content.Find.Execute "(^13[0-9.]{1,})", , , 1, , , , , , "\1`", 2

  92.         For Each i In .Paragraphs
  93.             With i.Range
  94.                 If Not .Information(12) Then
  95.                     If .Style Like "标题*" Then
  96.                         If .Text Like "*[。:;,、!?…—.:;,!?]?" Then .Characters.Last.Previous.Delete
  97.                     End If
  98.                     If .Style = "标题 1" Then
  99.                         a = a + 1
  100.                         b = 0
  101.                         If j = 0 Then
  102.                             With ActiveDocument.Range(Start:=.Characters(1).Start, End:=.Characters(InStr(.Text, "章")).End - 1)
  103.                                 .Delete
  104.                                 .Fields.Add Range:=i.Range, Text:="= " & a & " \* CHINESENUM3"
  105.                                 .Fields.Unlink
  106.                                 .InsertBefore Text:="第"
  107.                             End With
  108.                         ElseIf j = 1 Then
  109.                             ActiveDocument.Range(Start:=.Characters(2).Start, End:=.Characters(InStr(.Text, "章") - 1).End).Text = a
  110.                         End If
  111.                         If .Characters(InStr(.Text, "章") + 3).Text = vbCr Then .Characters.Last.Previous.InsertBefore Text:=" "
  112.                         .Characters(InStr(.Text, "章")).InsertAfter Text:=" "
  113.                     ElseIf .Style = "标题 2" Then
  114.                         b = b + 1
  115.                         c = 0
  116.                         ActiveDocument.Range(Start:=.Characters(1).Start, End:=.Characters(InStr(.Text, "`") - 1).End).Text = a & "." & b
  117.                     ElseIf .Style = "标题 3" Then
  118.                         c = c + 1
  119.                         d = 0
  120.                         ActiveDocument.Range(Start:=.Characters(1).Start, End:=.Characters(InStr(.Text, "`") - 1).End).Text = a & "." & b & "." & c
  121.                     ElseIf .Style = "标题 4" Then
  122.                         d = d + 1
  123.                         e = 0
  124.                         ActiveDocument.Range(Start:=.Characters(1).Start, End:=.Characters(InStr(.Text, "`") - 1).End).Text = a & "." & b & "." & c & "." & d
  125.                     ElseIf .Style = "标题 5" Then
  126.                         e = e + 1
  127.                         ActiveDocument.Range(Start:=.Characters(1).Start, End:=.Characters(InStr(.Text, "`") - 1).End).Text = a & "." & b & "." & c & "." & d & "." & e
  128.                     End If
  129.                 End If
  130.             End With
  131.         Next

  132.         .Content.Find.Execute "`", , , 0, , , , , , " ", 2

  133. '''All Format

  134.         With .Content
  135.             With .Font
  136.                 .Kerning = 0
  137.                 .DisableCharacterSpaceGrid = True
  138.             End With
  139.             With .ParagraphFormat
  140.                 .LineSpacing = LinesToPoints(1.5)
  141.                 .AutoAdjustRightIndent = False
  142.                 .DisableLineHeightGrid = True
  143.                 .KeepWithNext = False
  144.                 .KeepTogether = False
  145.             End With
  146.         End With

  147.         If .Characters(1).Text <> "第" Then
  148.             With .Paragraphs(1).Range
  149.                 .Style = wdStyleTitle
  150.                 .Font.Size = 26
  151.             End With
  152.         End If

  153.     End With

  154.     Selection.HomeKey 6

  155. End Sub
复制代码


评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2019-12-6 09:40 | 显示全部楼层
膜拜大神~虽然自己用不上,但是不明觉厉

TA的精华主题

TA的得分主题

发表于 2020-3-13 16:52 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2020-3-16 10:30 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
求教老师一下,我怎么把代码导入到WORD中呀

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-3-17 01:20 | 显示全部楼层
请新建一个宏,然后,按 Alt + F11 找到它,复制我的代码到它的下面即可。

TA的精华主题

TA的得分主题

发表于 2020-4-29 09:04 | 显示全部楼层
在word2019上运行会出现。“集合所要求的成员不存在”是怎么回事啊

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2020-10-15 18:54 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
留个记号,先收藏下,word的内置编号功能用得不好反而烦人,研究下这个代码

TA的精华主题

TA的得分主题

发表于 2021-2-1 18:57 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2021-10-10 13:15 来自手机 | 显示全部楼层
您好,我试了下有错误,第2级标题的1.1总是跟第一章在一排上是什么情况

TA的精华主题

TA的得分主题

发表于 2021-10-10 13:16 来自手机 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
shenjianrong163 发表于 2021-2-1 18:57
谢谢分享!

您是否在,我想请教下多级列表自动编号这个宏,我的老是出错
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-4-27 12:12 , Processed in 0.035666 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表