ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[求助] WORD文档批量设置格式

[复制链接]

TA的精华主题

TA的得分主题

发表于 2019-7-9 00:26 | 显示全部楼层 |阅读模式

我是专门在学校帮别人打印论文的,我想用VBA设置一个标准的论文格式,论文标准格式如下:


关于XXX问题的研讨 题目居中,三号黑体字

XXX XXX XXX 作者居中, 其后标明工作单位,所在省、市,邮编,4号楷体字

摘 要:XXXXXXXXXXXXXX “摘要”两字5号黑体,其余5号宋体

关键词:XXXX XXXX(RS) YYYY “关键词”三字用5号黑体,其余宋体5号


(正文)

一、XXXXXX 一级标题用4号黑体字(序号用一、二……依此类推)

1.XXXXXXX 二级标题用4号宋体字(序号用1、2……依此类推)

⑴xxxxxxxxx 三级标题用4号宋体字(序号用⑴、⑵、⑶……依此类推)

(正文内容)

yyyyyyyyyyyyyyyyyyyyyyyyy 正文用5号宋体

参考文献: 用5号黑体字

[1]XXX,XYY,XXX,XXXX,《XXXXXXXX》,2008,(1) 5号宋体

[2]Xxx、Xxx 译,《XXXXXXXX》,XX教育出版社,1998. 5号宋体

作者简介: 作者单位、电话、传真、电子信箱、通讯地址及邮政编码 5号宋体


请哪位帮一帮忙,最好能把一个文件夹内的所有文件都批量设置


TA的精华主题

TA的得分主题

发表于 2019-7-9 08:30 来自手机 | 显示全部楼层
一般人连样式和样式集都没咋用过,别提用vba去套用了

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-7-9 09:06 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2019-7-10 21:20 | 显示全部楼层
楼主,请将你所使用电脑的操作系统版本、Word版本说明一下,另外最好提供详细的论文样本(可以隐去一些人名及关键词汇),我按照你的要求试着编了一个宏,请测试:
  1. Sub 论文排版()
  2.     Dim r As Range, a As Range, i As Paragraph, n&, x As Section

  3.     ActiveWindow.ActivePane.View.Zoom.PageFit = wdPageFitBestFit

  4.     With ActiveDocument
  5.         For Each x In .Sections
  6.             With x.PageSetup
  7.                 If .Orientation = wdOrientPortrait Then
  8.                     .TopMargin = CentimetersToPoints(2.54)
  9.                     .BottomMargin = CentimetersToPoints(2.54)
  10.                     .LeftMargin = CentimetersToPoints(3.17)
  11.                     .RightMargin = CentimetersToPoints(3.17)
  12.                     .PageWidth = CentimetersToPoints(21)
  13.                     .PageHeight = CentimetersToPoints(29.7)
  14.                 Else
  15.                     .TopMargin = CentimetersToPoints(2.5)
  16.                     .BottomMargin = CentimetersToPoints(2.5)
  17.                     .LeftMargin = CentimetersToPoints(2.54)
  18.                     .RightMargin = CentimetersToPoints(2.54)
  19.                     .PageWidth = CentimetersToPoints(29.7)
  20.                     .PageHeight = CentimetersToPoints(21)
  21.                 End If
  22.                 .HeaderDistance = CentimetersToPoints(1.5)
  23.                 .FooterDistance = CentimetersToPoints(1.75)
  24.             End With
  25.         Next

  26.         .CopyStylesFromTemplate Template:=.AttachedTemplate.FullName

  27.         .Content.Find.Execute "[^13^11]", , , 1, , , , , , "^p", 2

  28.         With .Content.Find
  29.             .Execute "(", , , , , , , , , "(", 2
  30.             .Execute ")", , , , , , , , , ")", 2
  31.             .Execute "([一-﨩])(:)", , , 1, , , , , , "\1:", 2
  32.         End With

  33.         .Select
  34.     End With

  35.     With Selection
  36.         .ClearFormatting
  37.         CommandBars.FindControl(ID:=122).Execute
  38.         CommandBars.FindControl(ID:=123).Execute
  39.         .Font.Color = wdColorBlue
  40.         .ParagraphFormat.CharacterUnitFirstLineIndent = 2
  41.         Set r = .Range
  42.         Set a = .Range
  43.     End With

  44.     With r
  45.         For Each i In .Paragraphs
  46.             If Asc(i.Range) = 13 Then i.Range.Delete
  47.         Next

  48.         With .Find
  49.             .ClearFormatting
  50.             .Text = "^13[0-90-9]@[、..]"
  51.             .Forward = True
  52.             .MatchWildcards = True
  53.             Do While .Execute
  54.                 With .Parent
  55.                     .Characters.Last.Text = "."
  56.                     .CharacterWidth = wdWidthHalfWidth
  57.                     .SetRange Start:=.End, End:=a.End
  58.                 End With
  59.             Loop
  60.         End With

  61.         .SetRange Start:=a.Start, End:=a.End

  62.         Const s As String = "一二三四五六七八九十1234567890百零〇○"
  63.         For Each i In .Paragraphs
  64.             With i.Range
  65.                 n = 1
  66.                 Do While InStr(s, .Characters(n)) > 0
  67.                     n = n + 1
  68.                     If .Characters(n).Text = "、" Then
  69.                         .Style = wdStyleHeading1
  70.                         With .Font
  71.                             .NameFarEast = "黑体"
  72.                             .NameAscii = "Arial"
  73.                             .Size = 14
  74.                             .Bold = True
  75.                             .Color = wdColorRed
  76.                         End With
  77.                         .ParagraphFormat.CharacterUnitFirstLineIndent = 1.5
  78.                         Exit Do
  79.                     End If
  80.                     If .Characters(n).Text = "." Then
  81.                         .Style = wdStyleHeading2
  82.                         With .Font
  83.                             .NameFarEast = "宋体"
  84.                             .NameAscii = "Times New Roman"
  85.                             .Size = 14
  86.                             .Bold = True
  87.                             .Color = wdColorPink
  88.                         End With
  89.                         .ParagraphFormat.CharacterUnitFirstLineIndent = 1.5
  90.                         Exit Do
  91.                     End If
  92.                 Loop
  93.                 If .Text Like "[⑴⑵⑶⑷⑸⑹⑺⑻⑼⑽⑾⑵⑶⑷⑸⑹⑺⑻⑼⑽]*" Then
  94.                     .Style = wdStyleHeading3
  95.                     With .Font
  96.                         .NameFarEast = "宋体"
  97.                         .NameAscii = "Times New Roman"
  98.                         .Size = 14
  99.                         .Bold = True
  100.                         .Color = wdColorGreen
  101.                     End With
  102.                     .ParagraphFormat.CharacterUnitFirstLineIndent = 1.5
  103.                 ElseIf .Text Like "摘*要[::]*" Or .Text Like "关键词[::]*" Or .Text Like "参考文献[::]*" Then
  104.                     With ActiveDocument.Range(Start:=.Start, End:=.Characters(InStr(.Text, ":")).End)
  105.                         With .Font
  106.                             .NameFarEast = "黑体"
  107.                             .Bold = True
  108.                             .Color = wdColorOrange
  109.                         End With
  110.                     End With
  111.                 End If
  112.             End With
  113.         Next
  114.     End With

  115.     With Selection
  116.         With .Font
  117.             .Kerning = 0
  118.             .DisableCharacterSpaceGrid = True
  119.         End With
  120.         With .ParagraphFormat
  121.             .SpaceBeforeAuto = False
  122.             .SpaceAfterAuto = False
  123.             .SpaceBefore = 0
  124.             .SpaceAfter = 0
  125.             .LineSpacing = LinesToPoints(1.5)
  126.             .AutoAdjustRightIndent = False
  127.             .DisableLineHeightGrid = True
  128.             .KeepWithNext = False
  129.             .KeepTogether = False
  130.         End With

  131.         .HomeKey 6
  132.     End With

  133.     With ActiveDocument
  134.         With .Paragraphs(1).Range
  135.             With .Font
  136.                 .NameFarEast = "黑体"
  137.                 .Size = 16
  138.                 .Bold = True
  139.                 .Color = wdColorAutomatic
  140.             End With
  141.             With .ParagraphFormat
  142.                 .SpaceBefore = 24
  143.                 .SpaceAfter = 6
  144.             End With
  145.         End With
  146.         With .Paragraphs(2).Range
  147.             With .Font
  148.                 .NameFarEast = "楷体_GB2312"
  149.                 .Size = 14
  150.                 .Bold = True
  151.                 .Color = wdColorBrown
  152.             End With
  153.             With .ParagraphFormat
  154.                 .SpaceBefore = 6
  155.                 .SpaceAfter = 24
  156.             End With
  157.         End With

  158.         With .Range(Start:=0, End:=.Paragraphs(2).Range.End).ParagraphFormat
  159.             .CharacterUnitFirstLineIndent = 0
  160.             .FirstLineIndent = CentimetersToPoints(0)
  161.             .Alignment = wdAlignParagraphCenter
  162.         End With

  163. '        .Content.Font.Color = wdColorAutomatic'去掉注释全文变自动色
  164.     End With
  165. End Sub
复制代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-7-11 00:21 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
谢谢,非常满意

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-7-11 00:25 | 显示全部楼层
还请能补充一下标准的页面设置不

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-7-11 00:26 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2019-7-11 10:01 | 显示全部楼层
楼主,4 楼代码中,07-27这些行即为 页面设置 代码(默认A4纵向),可以自行修改。
如果想要公文一字线代码,可下载我的《集成版》代码,里面有《插入页码_公文一字线》宏。
另外,代码可以按 F8 键一行一行地自行调试。如有字体、段落格式不明白的地方,录制宏即可。

TA的精华主题

TA的得分主题

发表于 2019-7-11 10:01 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2019-7-11 10:05 | 显示全部楼层
楼主,我的《Word2003 & 2007 VBA 自动排版宏》(集成版)2019-7-9,我建议你也可以试着使用其中的代码,可以先试用 2003 的,再试用 2007 的,然后可以自行修改。
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

最新热点上一条 /1 下一条

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

GMT+8, 2024-4-26 01:11 , Processed in 0.041782 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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