Sub 公文自动排版()
' 设置页面参数(单位:厘米)
With ActiveDocument.PageSetup
.TopMargin = CentimetersToPoints(3.7) ' 上边距3.7cm
.BottomMargin = CentimetersToPoints(3.5) ' 下边距3.5cm
.LeftMargin = CentimetersToPoints(2.8) ' 左边距2.8cm
.RightMargin = CentimetersToPoints(2.6) ' 右边距2.6cm
.LineNumbering.Active = False ' 关闭行号
.Gutter = 0 ' 清除装订线边距
.PageHeight = CentimetersToPoints(29.7) ' A4纸高度29.7cm
.PageWidth = CentimetersToPoints(21) ' A4纸宽度21cm
End With
' 定义正文样式
Dim NormalStyle As Style
Set NormalStyle = ActiveDocument.Styles("正文")
With NormalStyle
With.Font
.Name = "仿宋_GB2312" ' 默认仿宋字体
.Size = 16 ' 三号字(1磅≈0.35mm,三号=15.6磅≈5.5mm)
.Spacing = -0.3 ' 紧缩0.3磅保证每行28字
.Bold = False ' 取消加粗
End With
With.ParagraphFormat
.Alignment = wdAlignParagraphJustify ' 两端对齐
' 取消首行缩进
.FirstLineIndent = 0
.LineSpacingRule = wdLineSpaceExactly ' 固定值
.LineSpacing = 28 ' 行距28磅保证每页22行
.KeepTogether = True ' 段落保持在同一页
.KeepWithNext = False ' 不与下一段落保持在同一页
End With
End With
' 处理一级标题(格式:"一、"开头)
Dim para As Paragraph
Dim i As Long
For i = ActiveDocument.Paragraphs.Count To 1 Step -1
Set para = ActiveDocument.Paragraphs(i)
If Trim(para.Range.Text) = "" Then
para.Range.Delete
ElseIf Left(para.Range.Text, 2) Like "[一二三四五六七八九十]、" Then
With para.Range
.Style = ActiveDocument.Styles("标题 1") ' 修正样式名称
With.Font
.Name = "方正黑体_GBK" ' 黑体标题
.Size = 16
.Spacing = 0 ' 取消紧缩
.Bold = False ' 取消加粗
End With
With.ParagraphFormat
' 取消首行缩进
.FirstLineIndent = 0
.SpaceBefore = 0 ' 段前间距
.SpaceAfter = 0 ' 段后间距
End With
End With
ElseIf para.Range.Text Like "([一二三四五六七八九十])*" Then ' 处理二级标题
With para.Range
.Style = ActiveDocument.Styles("标题 2") ' 修正样式名称
With.Font
.Name = "楷体_GB2312" ' 楷体标题
.Size = 16
.Spacing = 0
.Bold = False ' 取消加粗
End With
With.ParagraphFormat
' 取消首行缩进
.FirstLineIndent = 0
.SpaceBefore = 0
.SpaceAfter = 0
End With
End With
Else ' 其他段落应用正文样式
para.Range.Style = NormalStyle
End If
Next i
MsgBox "公文排版已完成!", vbInformation, "操作提示"
End Sub