Sub DocumentFormatting() Dim oDoc As Document Set oDoc = ActiveDocument ' 设置全局参数 With oDoc ' ========== 正文设置 ========== ' 正文题目样式(标题1) With .Styles(wdStyleHeading1).Font .Name = "黑体" .NameAscii = "Times New Roman" .Size = 22 End With With .Styles(wdStyleHeading1).ParagraphFormat .SpaceBefore = 17 .SpaceAfter = 17 .LineSpacingRule = wdLineSpaceExactly .LineSpacing = 28 .Alignment = wdAlignParagraphCenter End With
' 正文内容样式(正文) With .Styles(wdStyleNormal).Font .Name = "仿宋" .NameAscii = "Times New Roman" .Size = 16 End With With .Styles(wdStyleNormal).ParagraphFormat .LineSpacingRule = wdLineSpaceExactly .LineSpacing = 28 End With
' 一级标题样式(标题2) With .Styles(wdStyleHeading2).Font .Name = "仿宋" .NameAscii = "Times New Roman" .Size = 16 .Bold = True End With With .Styles(wdStyleHeading2).ParagraphFormat .LineSpacingRule = wdLineSpaceExactly .LineSpacing = 28 End With
' ========== 脚注设置 ========== With .Styles(wdStyleFootnoteText).Font .Name = "宋体" .NameAscii = "Times New Roman" .Size = 9 End With .Styles(wdStyleFootnoteText).ParagraphFormat.LineSpacing = 28
' ========== 图表设置 ========== ' 创建表标题样式 If Not StyleExists("表标题") Then .Styles.Add Name:="表标题", Type:=wdStyleTypeParagraph With .Styles("表标题").Font .Name = "黑体" .Size = 12 End With With .Styles("表标题").ParagraphFormat .SpaceBefore = 6 .Alignment = wdAlignParagraphCenter End With End If
' 创建图标题样式(同上) If Not StyleExists("图标题") Then .Styles.Add Name:="图标题", Type:=wdStyleTypeParagraph .Styles("图标题").Duplicate .Styles("表标题") End If
' 处理所有表格 Dim oTbl As Table For Each oTbl In .Tables ' 表格标题 If oTbl.Range.Previous(wdParagraph).Style <> "表标题" Then oTbl.Range.InsertBefore vbCr oTbl.Range.Previous(wdParagraph).Style = "表标题" End If ' 表格内容格式 With oTbl.Range .Font.Name = "仿宋" .Font.NameAscii = "Times New Roman" .Font.Size = 12 ' 第一行加粗 oTbl.Rows(1).Range.Font.Bold = True ' 第一列加粗 For Each oCell In oTbl.Columns(1).Cells oCell.Range.Font.Bold = True Next End With oTbl.Range.ParagraphFormat.LineSpacingRule = wdLineSpaceSingle Next
' ========== 数字/英文全局设置 ========== With .Styles(wdStyleNormal).Font .NameAscii = "Times New Roman" .NameOther = "Times New Roman" End With End With
MsgBox "排版已完成!", vbInformation End Sub
Function StyleExists(sStyleName As String) As Boolean On Error Resume Next StyleExists = (Trim(ActiveDocument.Styles(sStyleName).NameLocal) = sStyleName) End Function