百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

word公文自动排版VBA代码,拿走不谢

zhezhongyun 2025-09-01 19:14 26 浏览

Sub 文档初始化() '公文格式初始化

Selection.WholeStory '选择word 所有文档

Selection.ClearFormatting '文档格式清除


Selection.Range.HighlightColorIndex = wdNoHighlight '突出显示文本取消


With Selection.Paragraphs '段落设置

.Alignment = wdAlignParagraphLeft '左对齐

.LineSpacingRule = wdLineSpaceExactly '行距固定28.8

.LineSpacing = 28.8

.IndentFirstLineCharWidth 3 '首行缩进2个字符

End With


With Selection.Font '字体设置

.Name = "仿宋_GB2312" '字体名称

.Size = 16 '三号字体

.ColorIndex = wdBlack '黑色

End With

End Sub


Sub 标题正文设置()


With Selection.PageSetup '页面设置

.TopMargin = CentimetersToPoints(3.7) '顶端边距

.BottomMargin = CentimetersToPoints(3.5) '底端边距

.LeftMargin = CentimetersToPoints(2.8) '左边距

.RightMargin = CentimetersToPoints(2.6) '右边距

'.PageWidth = CentimetersToPoints(18.2) '页面宽度

'.PageHeight = CentimetersToPoints(25.7) '页面高度

End With


'字体设置

Dim title_reg, f_reg, s_reg, th_reg, fr_reg, k, mh, strA$

Set myRange = ActiveDocument.Content

' 正则表达式 获取文档内容

strA = myRange.Text

Set title_reg = CreateObject("vbscript.regexp")

Set f_reg = CreateObject("vbscript.regexp")

Set s_reg = CreateObject("vbscript.regexp")

Set th_reg = CreateObject("vbscript.regexp")


Selection.HomeKey unit:=wdStory '光标回到文章开头

t = 0

title_reg.Pattern = "\r\r"

'[^\r]除了换行符之外的所有字符


title_reg.Global = True

Set Title = title_reg.Execute(strA)

With Selection.Find

.ClearFormatting

.Text = Title.Item(0)

.Execute Forward:=True

Selection.HomeKey unit:=wdStory, Extend:=wdExtend

End With

'选择有两个换行符的至开头的所有段落

With Selection.Font

.Name = "方正小标宋简体"

.Size = 22

.ColorIndex = wdBlack

End With

With Selection.Paragraphs '设置行距

.FirstLineIndent = CentimetersToPoints(0) '取消首行缩进

.Alignment = wdAlignParagraphCenter '段落居中

.LineSpacingRule = wdLineSpaceExactly '行距固定

.LineSpacing =
Word.Application.LinesToPoints(2.3) '行距为2.3倍行距 一行距=12

End With

' 以下是设置一级标题

t1 = 0 '初始化t1,作为一级标题是否是一是二是三是的标记,如果是,则为1,不是则为0

Selection.HomeKey unit:=wdStory

f_reg.Pattern = "(一、|二、|三、|四、|五、|六、|七、|八、|九、|十、|十一、|十二、|十三、|十四、|十五、|十六、|十七、|十八、|十九、|二十、|二十一、|二十二、|二十三、|二十四、|二十五、|二十五、|二十五、|二十六、|二十七、|二十八、|二十九、|三十、)[^\r]*\r"

f_reg.Global = True

Set f_titles = f_reg.Execute(strA)

If f_titles.Count = 0 Then '如果一级标题是一是二是三是,则匹配

f_reg.Pattern = "(一是|二是|三是|四是|五是|六是|七是|八是|九是|十是|十一是|十二是|十三是|十四是|十五是|十六是|十七是|十八是|十九是|二十是|二十一是|二十二是|二十三是|二十四是|二十五是|二十六是|二十七是|二十八是|二十九是|三十是)([^。])*。"

Set f_titles = f_reg.Execute(strA)

t1 = 1

End If

For Each f_title In f_titles

With Selection.Find

.ClearFormatting

.Text = f_title.Value

Debug.Print "一级标题遍历项目:"; f_title.Value

.Execute Forward:=True

End With


With Selection.Font

.Name = "黑体"

.Size = "16"

.ColorIndex = wdBlack

End With

Selection.HomeKey unit:=wdStory

Next


' 以下是设置二级标题

If t1 = 0 Then 'p判断一级标题是否是一是二是三是的标记,如果是0,则不是一是二是三是,则执行,不是则不执行

t2 = 0

Selection.HomeKey unit:=wdStory

s_reg.Global = True

s_reg.Pattern = "((一)|(二)|(三)|(四)|(五)|(六)|(七)|(八)|(九)|(十)|(十一)|(十二)|(十三)|(十四)|(十五)|(十六)|(十七)|(十八)|(十九)|(二十)|(二十一)|(二十二)|(二十三)|(二十四)|(二十五)|(二十六)|(二十七)|(二十八)|(二十九)|(三十))([^。\r:])*[。|\r:]" '排除句号和段落符号查找所有,找到句号或段落符号后停止

Set s_titles = s_reg.Execute(strA)

If s_titles.Count = 0 Then '如果二级标题是一是二是三是,则匹配

s_reg.Pattern = "(一是|二是|三是|四是|五是|六是|七是|八是|九是|十是|十一是|十二是|十三是|十四是|十五是|十六是|十七是|十八是|十九是|二十是|二十一是|二十二是|二十三是|二十四是|二十五是|二十六是|二十七是|二十八是|二十九是|三十是)([^。])*。"

Set s_titles = s_reg.Execute(strA)

t2 = 1

End If


For Each s_title In s_titles

With Selection.Find

.ClearFormatting

.Text = s_title.Value

Debug.Print "二级标题遍历项目:"; s_title.Value

.Execute Forward:=True

End With


With Selection.Font

.Name = "楷体"

.Size = "16"

.ColorIndex = wdBlack

.Bold = True

End With

Selection.HomeKey unit:=wdStory

Next

End If


' 以下是设置三级标题

If t2 = 0 Then

Selection.HomeKey unit:=wdStory

th_reg.Global = True

th_reg.Pattern = "\r\d{1,2}\.([^。])*。"

Set th_titles = th_reg.Execute(strA)

If th_titles.Count = 0 Then '如果三级标题是一是二是三是,则匹配

th_reg.Pattern = "(一是|二是|三是|四是|五是|六是|七是|八是|九是|十是|十一是|十二是|十三是|十四是|十五是|十六是|十七是|十八是|十九是|二十是|二十一是|二十二是|二十三是|二十四是|二十五是|二十六是|二十七是|二十八是|二十九是|三十是)([^。])*。"

Set th_titles = th_reg.Execute(strA)

End If

For Each th_title In th_titles

With Selection.Find

.ClearFormatting

.Text = th_title.Value

Debug.Print "三级标题遍历项目:"; th_title.Value

.Execute Forward:=True

End With


With Selection.Font

.Bold = True

.ColorIndex = wdBlack

End With

Selection.HomeKey unit:=wdStory

Next


End If

End Sub


Sub 页码设置()

ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).PageNumbers.Add PageNumberAlignment:=wdAlignPageNumberCenter, FirstPage:=True


With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary) '进入页脚编辑状态

.Range.Font.Size = 15

.Range.Font.Name = "仿宋"

.Range.Collapse Direction:=wdCollapseEnd

End With


End Sub

Sub 删除页眉横线()

With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range '进入页脚编辑状态

.Delete '删除页眉中的内容

.ParagraphFormat.Borders(wdBorderBottom).LineStyle = wdLineStyleNone '段落下边框线

End With

End Sub

Sub 公文格式排版()

Call 文档初始化

Call 标题正文设置

Call 页码设置

Call 删除页眉横线

End Sub

相关推荐

perl基础——循环控制_principle循环

在编程中,我们往往需要进行不同情况的判断,选择,重复操作。这些时候我们需要对简单语句来添加循环控制变量或者命令。if/unless我们需要在满足特定条件下再执行的语句,可以通过if/unle...

CHAPTER 2 The Antechamber of M de Treville 第二章 特雷维尔先生的前厅

CHAPTER1TheThreePresentsofD'ArtagnantheElderCHAPTER2TheAntechamber...

CHAPTER 5 The King'S Musketeers and the Cardinal'S Guards 第五章 国王的火枪手和红衣主教的卫士

CHAPTER3TheAudienceCHAPTER5TheKing'SMusketeersandtheCardinal'SGuard...

CHAPTER 3 The Audience 第三章 接见

CHAPTER3TheAudienceCHAPTER3TheAudience第三章接见M.DeTrévillewasatt...

别搞印象流!数据说明谁才是外线防守第一人!

来源:Reddit译者:@assholeeric编辑:伯伦WhoarethebestperimeterdefendersintheNBA?Here'sagraphofStea...

V-Day commemorations prove anti-China claims hollow

People'sLiberationArmyhonorguardstakepartinthemilitaryparademarkingthe80thanniversary...

EasyPoi使用_easypoi api

EasyPoi的主要特点:1.设计精巧,使用简单2.接口丰富,扩展简单3.默认值多,writelessdomore4.springmvc支持,web导出可以简单明了使用1.easypoi...

关于Oracle数据库12c 新特性总结_oracle数据库12514

概述今天主要简单介绍一下Oracle12c的一些新特性,仅供参考。参考:http://docs.oracle.com/database/121/NEWFT/chapter12102.htm#NEWFT...

【开发者成长】JAVA 线上故障排查完整套路!

线上故障主要会包括CPU、磁盘、内存以及网络问题,而大多数故障可能会包含不止一个层面的问题,所以进行排查时候尽量四个方面依次排查一遍。同时例如jstack、jmap等工具也是不囿于一个方面的问题...

使用 Python 向多个地址发送电子邮件

在本文中,我们将演示如何使用Python编程语言向使用不同电子邮件地址的不同收件人发送电子邮件。具体来说,我们将向许多不同的人发送电子邮件。使用Python向多个地址发送电子邮件Python...

提高工作效率的--Linux常用命令,能够决解95%以上的问题

点击上方关注,第一时间接受干货转发,点赞,收藏,不如一次关注评论区第一条注意查看回复:Linux命令获取linux常用命令大全pdf+Linux命令行大全pdf为什么要学习Linux命令?1、因为Li...

linux常用系统命令_linux操作系统常用命令

系统信息arch显示机器的处理器架构dmidecode-q显示硬件系统部件-(SMBIOS/DMI)hdparm-i/dev/hda罗列一个磁盘的架构特性hdparm-tT/dev/s...

小白入门必知必会-PostgreSQL-15.2源码编译安装

一PostgreSQL编译安装1.1下载源码包在PostgreSQL官方主页https://www.postgresql.org/ftp/source/下载区选择所需格式的源码包下载。cd/we...

Linux操作系统之常用命令_linux系统常用命令详解

Linux操作系统一、常用命令1.系统(1)系统信息arch显示机器的处理器架构uname-m显示机器的处理器架构uname-r显示正在使用的内核版本dmidecode-q显示硬件系...

linux网络命名空间简介_linux 网络相关命令

此篇会以例子的方式介绍下linux网络命名空间。此例中会创建两个networknamespace:nsa、nsb,一个网桥bridge0,nsa、nsb中添加网络设备veth,网络设备间...