ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[求助] 优化整理文档代码

[复制链接]

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-8-9 13:47 | 显示全部楼层
413191246se 发表于 2019-8-9 09:56
请参见:http://club.excelhome.net/thread-1487647-1-1.html

学习了,谢谢!第一章还行,但是自定义没用得起来;附件是我一直要做的排版,原文是没排之前的,要求用代码达到效果文档,您是否试一下?

Desktop.zip

34 KB, 下载次数: 7

TA的精华主题

TA的得分主题

发表于 2019-8-10 09:50 | 显示全部楼层
* 楼主,你的空白通用模板已经被篡改了! 建议在 C 盘查找 Normal.dotm(打开高级搜索选项),找到几个删除几个,然后打开 Word,再退出重进。
* 拼凑了一些代码,由于行数过多,请 楼主 在运行时耐心等待(没完全按照 楼主 设想的格式,见谅!)。
  1. Sub aaab个性排版()
  2.     With ActiveDocument
  3.         .CopyStylesFromTemplate Template:=.AttachedTemplate.FullName
  4.         .Fields.Unlink
  5.         .ConvertNumbersToText
  6.         .Content.Find.Execute "[^13^11]", , , 1, , , , , , "^p", 2
  7.         .Select
  8.         With Selection
  9.             .Range.HighlightColorIndex = wdNoHighlight
  10.             .ClearFormatting
  11.             CommandBars.FindControl(ID:=122).Execute
  12.             CommandBars.FindControl(ID:=123).Execute
  13.         End With
  14. ''''
  15.         Dim t As Table
  16.         For Each t In .Tables
  17.             With t.Range.Rows
  18.                 .WrapAroundText = False
  19.                 .Alignment = wdAlignRowCenter
  20.             End With
  21.         Next
  22. '''
  23.         .Content.Find.Execute "([0-9]@)、", , , 1, , , , , , "\1.", 2
  24. '''
  25.         Dim i As Paragraph, n&
  26.         Const s As String = "一二三四五六七八九十1234567890百零〇○"
  27.         For Each i In .Paragraphs
  28.             With i.Range
  29.                 If Not .Information(12) Then
  30.                     n = 1
  31.                     If .Text Like "(*" Then n = 2
  32.                     Do While InStr(s, .Characters(n)) > 0
  33.                         n = n + 1
  34.                         If .Characters(n).Text = "、" Then .Style = wdStyleHeading2: Exit Do
  35.                         If .Characters(n).Text = "." Then .Style = wdStyleHeading3: Exit Do
  36.                     Loop
  37.                     If .Text Like "[ABCDE]、*" Then .Style = wdStyleHeading4
  38.                     '
  39.                     n = 1
  40.                     If .Text Like "第*" Then n = 2
  41.                     Do While InStr(s, .Characters(n)) > 0
  42.                         n = n + 1
  43.                         If .Characters(n).Text = "章" Then .Style = wdStyleHeading1: Exit Do
  44.                     Loop
  45.                 End If
  46.             End With
  47.         Next
  48. '''
  49.         'Title2345Font
  50.         For Each i In .Paragraphs
  51.             With i.Range
  52.                 If Not .Information(12) Then
  53.                     If .Style = "正文" Then
  54.                         .Font.Size = 14
  55.                         .ParagraphFormat.CharacterUnitFirstLineIndent = 9.25
  56.                     ElseIf .Style = "标题 1" Then
  57.                         .Font.NameFarEast = "华文中宋"
  58.                         With .ParagraphFormat
  59.                             .CharacterUnitFirstLineIndent = 0
  60.                             .FirstLineIndent = CentimetersToPoints(0)
  61.                         End With
  62.                         .InsertBreak Type:=0
  63.                     ElseIf .Style = "标题 2" Then
  64.                         .Font.NameFarEast = "黑体"
  65.                         .ParagraphFormat.CharacterUnitFirstLineIndent = 3
  66.                     ElseIf .Style = "标题 3" Then
  67.                         .Font.NameFarEast = "华文中宋"
  68.                         .Font.NameAscii = "Times New Roman"
  69.                         .ParagraphFormat.CharacterUnitFirstLineIndent = 5
  70.                     ElseIf .Style = "标题 4" Then
  71.                         .Font.NameAscii = "Times New Roman"
  72.                         .ParagraphFormat.CharacterUnitFirstLineIndent = 7.5
  73.                     End If
  74.                     If .Text Like "B、*" Then .ParagraphFormat.CharacterUnitFirstLineIndent = 9.21
  75.                 End If
  76.             End With
  77.         Next
  78. '''
  79.         'Title2345Color----字体颜色----可以删除!
  80.         For Each i In .Paragraphs
  81.             With i.Range
  82.                 If Not .Information(12) Then
  83.                     If .Style = "正文" Then
  84.                         .Font.Color = wdColorBlue
  85.                     ElseIf .Style = "标题 2" Then
  86.                         .Font.Color = wdColorRed
  87.                     ElseIf .Style = "标题 3" Then
  88.                         .Font.Color = wdColorPink
  89.                     ElseIf .Style = "标题 4" Then
  90.                         .Font.Color = wdColorGreen
  91.                     End If
  92.                 End If
  93.             End With
  94.         Next
  95. '''
  96.         With .Content
  97.             With .Find
  98.                 .Execute "(第[一二三四五六七八九十]@章)([!  ^s^t])*", , , 1, , , , , , "\1 \2", 2
  99.                 .Execute "(^13[ABCDE])、", , , 1, , , , , , "\1.", 2
  100.             End With
  101.             With .Font
  102.                 .Kerning = 0
  103.                 .DisableCharacterSpaceGrid = True
  104.             End With
  105.             With .ParagraphFormat
  106.                 .SpaceBeforeAuto = False
  107.                 .SpaceAfterAuto = False
  108.                 .SpaceBefore = 0
  109.                 .SpaceAfter = 0
  110.                 .LineSpacing = LinesToPoints(1.5)
  111.                 .AutoAdjustRightIndent = False
  112.                 .DisableLineHeightGrid = True
  113.                 .KeepWithNext = False
  114.                 .KeepTogether = False
  115.             End With
  116.         End With
  117. '''标题4绿色/可以删除
  118.         For Each i In .Paragraphs
  119.             With i.Range
  120.                 If Not .Information(12) Then
  121.                     If .Style = "标题 4" Then .Font.Color = wdColorGreen
  122.                 End If
  123.             End With
  124.         Next
  125. '''
  126.         For Each t In .Tables
  127.             With t.Range
  128.                 .Font.Size = 12
  129.                 .ParagraphFormat.Alignment = wdAlignParagraphCenter
  130.                 .ParagraphFormat.Space1
  131.                 .Cells.VerticalAlignment = wdCellAlignVerticalCenter
  132.                 With .Rows
  133.                     .HeightRule = wdRowHeightAtLeast
  134.                     .Height = CentimetersToPoints(0.9)
  135.                 End With
  136.                 .Next(4, 1).ParagraphFormat.SpaceBefore = 6
  137.             End With
  138.         Next
  139.         .Paragraphs(1).Range.Delete
  140.     End With
  141. ''''删除页眉横线
  142.     ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
  143.     Selection.ParagraphFormat.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
  144.     ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
  145. '''
  146.     ActiveWindow.ActivePane.View.Zoom.PageFit = wdPageFitBestFit
  147.     Selection.HomeKey 6
  148.     MsgBox "处理完毕!!!!!!!!!!!", 0 + 48
  149. End Sub
复制代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-8-12 10:08 | 显示全部楼层
413191246se 发表于 2019-8-10 09:50
* 楼主,你的空白通用模板已经被篡改了! 建议在 C 盘查找 Normal.dotm(打开高级搜索选项),找到几个删除 ...

谢谢,我研究研究
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-5-19 10:45 , Processed in 0.030052 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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