ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

word2019,标题格式化问题

[复制链接]

TA的精华主题

TA的得分主题

发表于 2024-1-11 14:42 | 显示全部楼层 |阅读模式
向各位请教一下标题格式化的问题。我根据守柔(SHOUROU)WORD 编程代码集“四) 段落样式与格式的应用”,调整代码如下,希望可以做到1-9级标题格式、段落、字体统一,一级标题为“第一章”格式。运行报错4608,数值超出范围。附件为测试文档。
代码如下:



Sub 标题格式化()
    Dim i As Paragraph, MyStr As String
    Application.ScreenUpdating = False
    MyStr = "第一二三四五六七八九十章"
    For Each i In Me.Paragraphs
        If i.Range Like "#.#.#.#.#.#.#.#.#*" = True Then
            i.Style = wdStyleHeading9
            With i.Range.Font
            .NameFarEast = "宋体"
            .NameAscii = "宋体"
            .Size = 12
            .Bold = True
            .Color = wdColorAutomatic
            End With
            With i.Range.ParagraphFormat
            .Space2
            .LeftIndent = CentimetersToPoints(0)
            .RightIndent = CentimetersToPoints(0)
            .CharacterUnitLeftIndent = 0
            .CharacterUnitRightIndent = 0
            End With
        ElseIf i.Range Like "#.#.#.#.#.#.#.#*" = True Then
            i.Style = wdStyleHeading8
            With i.Range.Font
            .NameFarEast = "宋体"
            .NameAscii = "宋体"
            .Size = 12
            .Bold = True
            .Color = wdColorAutomatic
            End With
            With i.Range.ParagraphFormat
            .Space2
            .LeftIndent = CentimetersToPoints(0)
            .RightIndent = CentimetersToPoints(0)
            .CharacterUnitLeftIndent = 0
            .CharacterUnitRightIndent = 0
            End With
        ElseIf i.Range Like "#.#.#.#.#.#.#*" = True Then
            i.Style = wdStyleHeading7
            With i.Range.Font
            .NameFarEast = "宋体"
            .NameAscii = "宋体"
            .Size = 12
            .Bold = True
            .Color = wdColorAutomatic
            End With
            With i.Range.ParagraphFormat
            .Space2
            .LeftIndent = CentimetersToPoints(0)
            .RightIndent = CentimetersToPoints(0)
            .CharacterUnitLeftIndent = 0
            .CharacterUnitRightIndent = 0
            End With
        ElseIf i.Range Like "#.#.#.#.#.#*" = True Then
            i.Style = wdStyleHeading6
            With i.Range.Font
            .NameFarEast = "宋体"
            .NameAscii = "宋体"
            .Size = 12
            .Bold = True
            .Color = wdColorAutomatic
            End With
            With i.Range.ParagraphFormat
            .Space2
            .LeftIndent = CentimetersToPoints(0)
            .RightIndent = CentimetersToPoints(0)
            .CharacterUnitLeftIndent = 0
            .CharacterUnitRightIndent = 0
            End With
        ElseIf i.Range Like "#.#.#.#.#*" = True Then
            i.Style = wdStyleHeading5
            With i.Range.Font
            .NameFarEast = "宋体"
            .NameAscii = "宋体"
            .Size = 12
            .Bold = True
            .Color = wdColorAutomatic
            End With
            With i.Range.ParagraphFormat
            .Space2
            .LeftIndent = CentimetersToPoints(0)
            .RightIndent = CentimetersToPoints(0)
            .CharacterUnitLeftIndent = 0
            .CharacterUnitRightIndent = 0
            End With
        ElseIf i.Range Like "#.#.#.#*" = True Then
            i.Style = wdStyleHeading4
            With i.Range.Font
            .NameFarEast = "宋体"
            .NameAscii = "宋体"
            .Size = 14
            .Bold = True
            .Color = wdColorAutomatic
            End With
            With i.Range.ParagraphFormat
            .Space2
            .LeftIndent = CentimetersToPoints(0)
            .RightIndent = CentimetersToPoints(0)
            .CharacterUnitLeftIndent = 0
            .CharacterUnitRightIndent = 0
            End With
        ElseIf i.Range Like "#.#.#*" = True Then
            i.Style = wdStyleHeading3
            With i.Range.Font
            .NameFarEast = "宋体"
            .NameAscii = "宋体"
            .Size = 15
            .Bold = True
            .Color = wdColorAutomatic
            End With
            With i.Range.ParagraphFormat
            .Space2
            .LeftIndent = CentimetersToPoints(0)
            .RightIndent = CentimetersToPoints(0)
            .CharacterUnitLeftIndent = 0
            .CharacterUnitRightIndent = 0
            End With
        ElseIf i.Range Like "#.#*" = True Then
            i.Style = wdStyleHeading2
            With i.Range.Font
            .NameFarEast = "宋体"
            .NameAscii = "宋体"
            .Size = 16
            .Bold = True
            .Color = wdColorAutomatic
            End With
            With i.Range.ParagraphFormat
            .Space2
            .LeftIndent = CentimetersToPoints(0)
            .RightIndent = CentimetersToPoints(0)
            .CharacterUnitLeftIndent = 0
            .CharacterUnitRightIndent = 0
            End With
        ElseIf InStr(MyStr, Me.Range(i.Range.Start, i.Range.Start + 4).Text) > 0 Then
            i.Style = wdStyleHeading1
            With i.Range.Font
            .NameFarEast = "宋体"
            .NameAscii = "宋体"
            .Size = 22
            .Bold = True
            .Color = wdColorAutomatic
            End With
            With i.Range.ParagraphFormat
            .Space2
            .LeftIndent = CentimetersToPoints(0)
            .RightIndent = CentimetersToPoints(0)
            .CharacterUnitLeftIndent = 0
            .CharacterUnitRightIndent = 0
            End With
        Else
            i.Style = wdStyleNormal
            With i.Range.Font
            .NameFarEast = "宋体"
            .NameAscii = "宋体"
            .Size = 10.5
            .Bold = False
            .Color = wdColorAutomatic
            End With
            With i.Range.ParagraphFormat
            .Space2
            .IndentFirstLineCharWidth 2
            .CharacterUnitLeftIndent = 0
            .CharacterUnitRightIndent = 0
            End With
        End If
    Next
    Application.ScreenUpdating = True
End Sub



test.zip

387.43 KB, 下载次数: 3

TA的精华主题

TA的得分主题

发表于 2024-1-12 02:37 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
* 请将代码复制到空白文档后,全选,剪切,再粘贴到 VBE 中,以防乱码。代码仅供参考!
  1. Sub TitleStyle999()
  2.     Const ChnNum As String = "[一二三四五六七八九十百千零〇○Oo00Oo]@"
  3.     Dim i As Paragraph, n&

  4.     ActiveDocument.Content.Find.Execute "(^13第)" & ChnNum & "(章)", , , 1, , , , , , "\1一\2 ", 2

  5.     For Each i In ActiveDocument.Paragraphs
  6.         With i.Range
  7.             If .Text Like "#.#.#.#.#.#.#.#.#*" Then
  8.                 .Style = wdStyleHeading9
  9.                 GoSub sk
  10.             ElseIf .Text Like "#.#.#.#.#.#.#.#*" Then
  11.                 .Style = wdStyleHeading8
  12.                 GoSub sk
  13.             ElseIf .Text Like "#.#.#.#.#.#.#*" Then
  14.                 .Style = wdStyleHeading7
  15.                 GoSub sk
  16.             ElseIf .Text Like "#.#.#.#.#.#*" Then
  17.                 .Style = wdStyleHeading6
  18.                 GoSub sk
  19.             ElseIf .Text Like "#.#.#.#.#*" Then
  20.                 .Style = wdStyleHeading5
  21.                 GoSub sk
  22.             ElseIf .Text Like "#.#.#.#*" Then
  23.                 .Style = wdStyleHeading4
  24.                 GoSub sk
  25.                 .Font.Size = 14
  26.             ElseIf i.Range Like "#.#.#*" Then
  27.                 .Style = wdStyleHeading3
  28.                 GoSub sk
  29.                 .Font.ColorIndex = wdGreen
  30.                 .Font.Size = 15
  31.             ElseIf .Text Like "#.#*" Then
  32.                 .Style = wdStyleHeading2
  33.                 GoSub sk
  34.                 .Font.ColorIndex = wdPink
  35.                 .Font.Size = 16
  36.             ElseIf .Text Like "第一章*" Then
  37.                 .Style = wdStyleHeading1
  38.                 GoSub sk
  39.                 .Font.ColorIndex = wdRed
  40.                 .Font.Size = 22
  41.                 n = n + 1
  42.                 .Characters(1).Delete
  43.                 .Characters(1).Delete
  44.                 .Fields.Add Range:=.Paragraphs(1).Range, Text:="= " & n & " \* CHINESENUM3"
  45.                 .Fields.Unlink
  46.                 .InsertBefore Text:="第"
  47.             Else
  48.                 .Style = wdStyleNormal
  49.                 GoSub sk
  50.                 .Font.ColorIndex = wdBlue
  51.                 .Font.Size = 10.5
  52.             End If
  53.         End With
  54.     Next
  55.     Exit Sub

  56. sk:
  57.     With i.Range
  58.         With .Font
  59.             .NameFarEast = "宋体"
  60.             .NameAscii = "Times New Roman"
  61.             .Size = 12
  62.             .Bold = True
  63.             .Color = wdColorAutomatic
  64.         End With
  65.         With .ParagraphFormat
  66.             .Space2
  67.             .LeftIndent = CentimetersToPoints(0)
  68.             .RightIndent = CentimetersToPoints(0)
  69.             .CharacterUnitLeftIndent = 0
  70.             .CharacterUnitRightIndent = 0
  71.         End With
  72.     End With
  73.     Return
  74. End Sub
复制代码

评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-1-12 09:47 | 显示全部楼层
413191246se 发表于 2024-1-12 02:37
* 请将代码复制到空白文档后,全选,剪切,再粘贴到 VBE 中,以防乱码。代码仅供参考!

感谢您的回复。
运行代码后,有点小问题,一级标题格式没有实现,另外还删除了文字。
谢谢!

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-1-12 13:49 | 显示全部楼层
非常感谢413191246se,以下是我调整后的代码,实现了我需要的效果,感谢!
  1. Sub 标题格式化()

  2.     Const ChnNum As String = "[一二三四五六七八九十百千零〇○Oo00Oo]@"
  3.     Dim i As Paragraph, n&

  4.     ActiveDocument.Content.Find.Execute "(^13第)" & ChnNum & "(章)", , , 1, , , , , , "\1一\2 ", 2

  5.     For Each i In ActiveDocument.Paragraphs
  6.         With i.Range
  7.             If .Text Like "#.#.#.#.#.#.#.#.#*" Then
  8.                 .Style = wdStyleHeading9
  9.                 With .Font
  10.                 .NameFarEast = "宋体"
  11.                 .NameAscii = "宋体"
  12.                 .Size = 12
  13.                 .Bold = True
  14.                 .Color = wdColorAutomatic
  15.                 End With
  16.                 With .ParagraphFormat
  17.                 .Space2
  18.                 .LeftIndent = CentimetersToPoints(0)
  19.                 .RightIndent = CentimetersToPoints(0)
  20.                 .Alignment = wdAlignParagraphJustify
  21.                 .CharacterUnitLeftIndent = 0
  22.                 .CharacterUnitRightIndent = 0
  23.                 End With
  24.             ElseIf .Text Like "#.#.#.#.#.#.#.#*" Then
  25.                 .Style = wdStyleHeading8
  26.                 With .Font
  27.                 .NameFarEast = "宋体"
  28.                 .NameAscii = "宋体"
  29.                 .Size = 12
  30.                 .Bold = True
  31.                 .Color = wdColorAutomatic
  32.                 End With
  33.                 With .ParagraphFormat
  34.                 .Space2
  35.                 .LeftIndent = CentimetersToPoints(0)
  36.                 .RightIndent = CentimetersToPoints(0)
  37.                 .Alignment = wdAlignParagraphJustify
  38.                 .CharacterUnitLeftIndent = 0
  39.                 .CharacterUnitRightIndent = 0
  40.                 End With
  41.             ElseIf .Text Like "#.#.#.#.#.#.#*" Then
  42.                 .Style = wdStyleHeading7
  43.                 With .Font
  44.                 .NameFarEast = "宋体"
  45.                 .NameAscii = "宋体"
  46.                 .Size = 12
  47.                 .Bold = True
  48.                 .Color = wdColorAutomatic
  49.                 End With
  50.                 With .ParagraphFormat
  51.                 .Space2
  52.                 .LeftIndent = CentimetersToPoints(0)
  53.                 .RightIndent = CentimetersToPoints(0)
  54.                 .Alignment = wdAlignParagraphJustify
  55.                 .CharacterUnitLeftIndent = 0
  56.                 .CharacterUnitRightIndent = 0
  57.                 End With
  58.             ElseIf .Text Like "#.#.#.#.#.#*" Then
  59.                 .Style = wdStyleHeading6
  60.                 With .Font
  61.                 .NameFarEast = "宋体"
  62.                 .NameAscii = "宋体"
  63.                 .Size = 12
  64.                 .Bold = True
  65.                 .Color = wdColorAutomatic
  66.                 End With
  67.                 With .ParagraphFormat
  68.                 .Space2
  69.                 .LeftIndent = CentimetersToPoints(0)
  70.                 .RightIndent = CentimetersToPoints(0)
  71.                 .Alignment = wdAlignParagraphJustify
  72.                 .CharacterUnitLeftIndent = 0
  73.                 .CharacterUnitRightIndent = 0
  74.                 End With
  75.             ElseIf .Text Like "#.#.#.#.#*" Then
  76.                 .Style = wdStyleHeading5
  77.                 With .Font
  78.                 .NameFarEast = "宋体"
  79.                 .NameAscii = "宋体"
  80.                 .Size = 12
  81.                 .Bold = True
  82.                 .Color = wdColorAutomatic
  83.                 End With
  84.                 With .ParagraphFormat
  85.                 .Space2
  86.                 .LeftIndent = CentimetersToPoints(0)
  87.                 .RightIndent = CentimetersToPoints(0)
  88.                 .Alignment = wdAlignParagraphJustify
  89.                 .CharacterUnitLeftIndent = 0
  90.                 .CharacterUnitRightIndent = 0
  91.                 End With
  92.             ElseIf .Text Like "#.#.#.#*" Then
  93.                 .Style = wdStyleHeading4
  94.                 With .Font
  95.                 .NameFarEast = "宋体"
  96.                 .NameAscii = "宋体"
  97.                 .Size = 14
  98.                 .Bold = True
  99.                 .Color = wdColorAutomatic
  100.                 End With
  101.                 With .ParagraphFormat
  102.                 .Space2
  103.                 .LeftIndent = CentimetersToPoints(0)
  104.                 .RightIndent = CentimetersToPoints(0)
  105.                 .Alignment = wdAlignParagraphJustify
  106.                 .CharacterUnitLeftIndent = 0
  107.                 .CharacterUnitRightIndent = 0
  108.                 End With
  109.             ElseIf i.Range Like "#.#.#*" Then
  110.                 .Style = wdStyleHeading3
  111.                 With .Font
  112.                 .NameFarEast = "宋体"
  113.                 .NameAscii = "宋体"
  114.                 .Size = 15
  115.                 .Bold = True
  116.                 .Color = wdColorAutomatic
  117.                 End With
  118.                 With .ParagraphFormat
  119.                 .Space2
  120.                 .LeftIndent = CentimetersToPoints(0)
  121.                 .RightIndent = CentimetersToPoints(0)
  122.                 .Alignment = wdAlignParagraphJustify
  123.                 .CharacterUnitLeftIndent = 0
  124.                 .CharacterUnitRightIndent = 0
  125.                 End With
  126.             ElseIf .Text Like "#.#*" Then
  127.                 .Style = wdStyleHeading2
  128.                 With .Font
  129.                 .NameFarEast = "宋体"
  130.                 .NameAscii = "宋体"
  131.                 .Size = 16
  132.                 .Bold = True
  133.                 .Color = wdColorAutomatic
  134.                 End With
  135.                 With .ParagraphFormat
  136.                 .Space2
  137.                 .LeftIndent = CentimetersToPoints(0)
  138.                 .RightIndent = CentimetersToPoints(0)
  139.                 .Alignment = wdAlignParagraphJustify
  140.                 .CharacterUnitLeftIndent = 0
  141.                 .CharacterUnitRightIndent = 0
  142.                 End With
  143.             ElseIf .Text Like "第一章*" Then
  144.                 .Style = wdStyleHeading1
  145.                 With .Font
  146.                 .NameFarEast = "宋体"
  147.                 .NameAscii = "宋体"
  148.                 .Size = 22
  149.                 .Bold = True
  150.                 .Color = wdColorAutomatic
  151.                 End With
  152.                 With .ParagraphFormat
  153.                 .Space2
  154.                 .LeftIndent = CentimetersToPoints(0)
  155.                 .RightIndent = CentimetersToPoints(0)
  156.                 .Alignment = wdAlignParagraphJustify
  157.                 .CharacterUnitLeftIndent = 0
  158.                 .CharacterUnitRightIndent = 0
  159.                 End With
  160.             Else
  161.                 .Style = wdStyleNormal
  162.                 With .Font
  163.                 .NameFarEast = "宋体"
  164.                 .NameAscii = "宋体"
  165.                 .Size = 10.5
  166.                 .Bold = True
  167.                 .Color = wdColorAutomatic
  168.                 End With
  169.                 With .ParagraphFormat
  170.                 .Space2
  171.                 .IndentFirstLineCharWidth 2
  172.                 .Alignment = wdAlignParagraphJustify
  173.                 .CharacterUnitLeftIndent = 0
  174.                 .CharacterUnitRightIndent = 0
  175.                 End With
  176.             End If
  177.         End With
  178.     Next
  179.     End Sub
复制代码


TA的精华主题

TA的得分主题

发表于 2024-1-12 20:06 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
* 楼主,你好!——调整后达到了自己的目的,很好!
* 但我建议你,也应该注意代码简洁的问题,用 Gosub ... Return 结构共同设置了格式后,再分别设置一下格式,这样就精简了代码,也达到了目的。
* 再比如:西文和中文都是宋体,这样只须用 .Name = “宋体” 这句代码就行了,不必两行。

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-1-12 22:30 | 显示全部楼层
413191246se 发表于 2024-1-12 20:06
* 楼主,你好!——调整后达到了自己的目的,很好!
* 但我建议你,也应该注意代码简洁的问题,用 Gosub . ...

多谢给予指正

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-1-23 11:08 | 显示全部楼层
413191246se 发表于 2024-1-12 20:06
* 楼主,你好!——调整后达到了自己的目的,很好!
* 但我建议你,也应该注意代码简洁的问题,用 Gosub . ...

老师您好!我想问一下以下这句代码的含义
ActiveDocument.Content.Find.Execute "(^13第)" & ChnNum & "(章)", , , 1, , , , , , "\1一\2 ", 2

运行完毕后,我发现包括”第二章“都被改成”第一章“了。

盼复,多谢了!

TA的精华主题

TA的得分主题

发表于 2024-1-23 20:23 | 显示全部楼层
包昆 发表于 2024-1-12 13:49
非常感谢413191246se,以下是我调整后的代码,实现了我需要的效果,感谢!

if else间的代码可以写成sub过程调用,传入参数就可以了,避免代码的重复书写。

TA的精华主题

TA的得分主题

发表于 2024-1-23 20:45 | 显示全部楼层
变成“第一章”后,这样循环遍历段落的时候,只查找"第一章"就行了,方便简单,再用 n 加上域转换为汉字大写数目,如“第十一章”这样的形式,因为查找时不方便。

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-1-25 09:51 | 显示全部楼层
413191246se 发表于 2024-1-23 20:45
变成“第一章”后,这样循环遍历段落的时候,只查找"第一章"就行了,方便简单,再用 n 加上域转换为汉字大 ...

请教一下,如果不更改为第一章,保持原先的内容,需要如何调整?
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-4-27 19:46 , Processed in 0.046359 second(s), 17 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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