ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[求助] 二十多种样式设置的代码简化??

[复制链接]

TA的精华主题

TA的得分主题

发表于 2020-6-6 23:48 | 显示全部楼层 |阅读模式
请问一下需要设置二十多种样式,我写的代码太长了,
下面列出了一部分
感觉每个样式的代码都差不多,

就只有样式名称、字体、字号、行距这些不同
能不能把代码简化一下,
能不能实现类似于输入“样式名称、字体、字号、行距”这些作为参数,
就可以完成设置的呀~
  1. Sub 设置国论样式()
  2.    
  3.     On Error Resume Next
  4.    
  5.     '删除所有自定义样式
  6.     Dim i As Style
  7.     For Each i In ThisDocument.Styles
  8.         On Error Resume Next
  9.         i.Delete
  10.         Err.Clear
  11.     Next i
  12.    
  13.     '期刊标题样式
  14.     Application.ScreenUpdating = False '关闭屏幕更新
  15.     ActiveDocument.Styles.Add Name:="期刊标题", Type:=wdStyleTypeParagraph
  16.     ActiveDocument.Styles("期刊标题").AutomaticallyUpdate = False
  17.     With ActiveDocument.Styles("期刊标题").Font
  18.         .Color = wdColorAutomatic
  19.         .NameFarEast = "黑体"
  20.         .NameAscii = "Times New Roman"
  21.         .Size = 22  '字号,请输入对应数字
  22.         .Bold = 0 '加粗为1,不加粗为0
  23.     End With
  24.     With ActiveDocument.Styles("期刊标题").ParagraphFormat
  25.         .LeftIndent = CentimetersToPoints(0)
  26.         .Alignment = wdAlignParagraphCenter
  27.         '.CharacterUnitFirstLineIndent = 0
  28.         .LineUnitBefore = 0  '段前为0.8行
  29.         .LineUnitAfter = 0 '段后为0.5行
  30.         .LineSpacingRule = wdLineSpaceSingle
  31.     End With

  32.     '标题1样式
  33.     With ActiveDocument.Styles(wdStyleHeading1).Font
  34.         .Color = wdColorAutomatic
  35.         .NameFarEast = "宋体"
  36.         .NameAscii = "Times New Roman"
  37.         .Size = 14  '字号,请输入对应数字
  38.         .Bold = 1 '加粗为1,不加粗为0
  39.     End With
  40.     With ActiveDocument.Styles(wdStyleHeading1).ParagraphFormat
  41.         .LeftIndent = CentimetersToPoints(0)
  42.         .Alignment = wdAlignParagraphCenter
  43.         .CharacterUnitFirstLineIndent = 0
  44.         .LineUnitBefore = 0.5  '段前为0.8行
  45.         .LineUnitAfter = 0.5 '段后为0.5行
  46.         .LineSpacingRule = wdLineSpaceExactly
  47.         .LineSpacing = 17
  48.     End With
  49.    
  50.     '标题2样式
  51.     With ActiveDocument.Styles(wdStyleHeading2).Font
  52.         .Color = wdColorAutomatic
  53.         .NameFarEast = "宋体"
  54.         .NameAscii = "Times New Roman"
  55.         .Size = 12  '字号,请输入对应数字
  56.         .Bold = 1 '加粗为1,不加粗为0
  57.     End With
  58.     With ActiveDocument.Styles(wdStyleHeading2).ParagraphFormat
  59.         .LeftIndent = CentimetersToPoints(0)
  60.         .Alignment = wdAlignParagraphJustify
  61.         .CharacterUnitFirstLineIndent = 2
  62.         .LineUnitBefore = 0.5  '段前为0.8行
  63.         .LineUnitAfter = 0.5 '段后为0.5行
  64.         .LineSpacingRule = wdLineSpaceExactly
  65.         .LineSpacing = 17
  66.     End With
  67.    
  68.     '标题3样式
  69.     With ActiveDocument.Styles(wdStyleHeading3).Font
  70.         .Color = wdColorAutomatic
  71.         .NameFarEast = "宋体"
  72.         .NameAscii = "Times New Roman"
  73.         .Size = 12  '字号,请输入对应数字
  74.         .Bold = 1 '加粗为1,不加粗为0
  75.     End With
  76.     With ActiveDocument.Styles(wdStyleHeading3).ParagraphFormat
  77.         .LeftIndent = CentimetersToPoints(0)
  78.         .Alignment = wdAlignParagraphJustify
  79.         .CharacterUnitFirstLineIndent = 2
  80.         .LineUnitBefore = 0  '段前为0.8行
  81.         .LineUnitAfter = 0 '段后为0.5行
  82.         .LineSpacingRule = wdLineSpaceExactly
  83.         .LineSpacing = 17
  84.     End With
  85.    
  86.     '标题4样式
  87.     With ActiveDocument.Styles(wdStyleHeading4).Font
  88.         .Color = wdColorAutomatic
  89.         .NameFarEast = "宋体"
  90.         .NameAscii = "Times New Roman"
  91.         .Size = 12  '字号,请输入对应数字
  92.         .Bold = 1 '加粗为1,不加粗为0
  93.     End With
  94.     With ActiveDocument.Styles(wdStyleHeading4).ParagraphFormat
  95.         .LeftIndent = CentimetersToPoints(0)
  96.         .Alignment = wdAlignParagraphJustify
  97.         .CharacterUnitFirstLineIndent = 2
  98.         .LineUnitBefore = 0  '段前为0.8行
  99.         .LineUnitAfter = 0 '段后为0.5行
  100.         .LineSpacingRule = wdLineSpaceExactly
  101.         .LineSpacing = 17
  102.     End With
  103.    
  104.     '期刊图表标题样式
  105.     ActiveDocument.Styles.Add Name:="期刊图表标题", Type:=wdStyleTypeParagraph
  106.     ActiveDocument.Styles("期刊图表标题").AutomaticallyUpdate = False
  107.     With ActiveDocument.Styles("期刊图表标题").Font
  108.         .Color = wdColorAutomatic
  109.         .NameFarEast = "宋体"
  110.         .NameAscii = "Times New Roman"
  111.         .Size = 12  '字号,请输入对应数字
  112.         .Bold = 1 '加粗为1,不加粗为0
  113.     End With
  114.     With ActiveDocument.Styles("期刊图表标题").ParagraphFormat
  115.         .LeftIndent = CentimetersToPoints(0)
  116.         .OutlineLevel = wdOutlineLevelBodyText
  117.         .Alignment = wdAlignParagraphCenter
  118.         .CharacterUnitFirstLineIndent = 0
  119.         .LineUnitBefore = 0
  120.         .LineUnitAfter = 0
  121.         .LineSpacingRule = wdLineSpaceExactly
  122.         .LineSpacing = 17
  123.     End With
  124.    
  125.     '表格内容样式
  126.     ActiveDocument.Styles.Add Name:="期刊表格内容", Type:=wdStyleTypeParagraph
  127.     ActiveDocument.Styles("期刊表格内容").AutomaticallyUpdate = False
  128.     With ActiveDocument.Styles("期刊表格内容").Font
  129.         .Color = wdColorAutomatic
  130.         .NameFarEast = "宋体"
  131.         .NameAscii = "Times New Roman"
  132.         .Size = 9  '字号,请输入对应数字
  133.         .Bold = 0 '加粗为1,不加粗为0
  134.     End With
  135.    
  136.     Application.ScreenUpdating = True
  137.     MsgBox "设置样式成功"
  138. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2020-6-7 00:09 | 显示全部楼层
楼主,样式不少,但请提供一下示例文本(demo.docx),然后我们好运行你的代码呀!

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-6-7 08:37 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
413191246se 发表于 2020-6-7 00:09
楼主,样式不少,但请提供一下示例文本(demo.docx),然后我们好运行你的代码呀!

这只是用来设置样式的,随便一个空的word的文档都可以吧~我不太理解是什么意思

TA的精华主题

TA的得分主题

发表于 2020-6-7 23:30 | 显示全部楼层
楼主,运行你的代码,提示成功!但文档格式未有任何变化,也就是说,你的代码并不能排版。
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-4-18 10:32 , Processed in 0.036359 second(s), 8 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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