|
本帖最后由 乐乐2006201505 于 2018-5-5 21:13 编辑
下面代码实现了设置各级别标题字体效果,但是一级标题段中有句号就会出错。还有把最后的句号删除。望哪位大师修改一下,同时实现其他要求。具体要求在第一个附件中,非常感谢!
ption Explicit
Sub 公文Text()
Dim doc As Document, i As Paragraph
Set doc = ActiveDocument
Selection.WholeStory
Selection.ClearFormatting
CommandBars.FindControl(ID:=122).Execute
CommandBars.FindControl(ID:=123).Execute
删除空行
Selection.WholeStory
正文样式16
Title2345
For Each i In doc.Paragraphs
With i.Range
If .Style = "标题 2" Then
.Font.Name = "黑体"
.Font.Name = "Times New Roman"
ElseIf .Style = "标题 3" Then
.Font.Name = "楷体_GB2312"
.Font.Name = "Times New Roman"
ElseIf .Style = "标题 4" Then
.Font.Name = "仿宋_GB2312"
.Font.Name = "Times New Roman"
ElseIf .Style = "标题 5" Then
.Font.Name = "仿宋_GB2312"
.Font.Name = "Times New Roman"
ElseIf i.Range Like "[一二三四五六七八九十][,、 ]*" Or i.Range Like "[一二三四五六七八九十][是要]*" Then
If Not i.Range Like "*。" & vbCr Then i.Range.Characters.Last.InsertBefore Text:="。"
GoTo diyi
End If
If .Style <> "正文" Then
If .Sentences.Count = 1 Then
If i.Range Like "*[。:;,、!?…—.:;,!?]" & vbCr Then i.Range.Characters.Last.Previous.Delete
Else
diyi:
With .Font
.Name = "仿宋_GB2312"
.Name = "Times New Roman"
.Bold = False
' .Color = wdColorBlue
End With
With .Sentences(1).Font
.Bold = True
' .Color = wdColorBrown
End With
End If
End If
End With
Next
特殊样式
End Sub
Sub 正文样式16()
With Selection
.ClearFormatting
With .Font
.Name = "仿宋_GB2312"
.Name = "Times New Roman"
.Size = 16
' .Color = wdColorBlue
.Kerning = 0
.DisableCharacterSpaceGrid = True
End With
With .ParagraphFormat
.LineSpacing = LinesToPoints(1.25)
.CharacterUnitFirstLineIndent = 2
.AutoAdjustRightIndent = False
.DisableLineHeightGrid = True
End With
End With
End Sub
Sub Title2345()
Dim mt, reg As Object, n&, m&, L&, ostr$, sr$, r1$, r2$, r3$, r4$
ostr$ = Replace(ActiveDocument.Content, Chr(7), "")
sr$ = "一二三四五六七八九十百零千〇"
r1$ = "^[" & sr & "]+、": r2$ = "^[((]\s*[" & sr & "]+\s*[))]"
r3$ = "^\d+[、..]": r4$ = "^[((]\s*\d+\s*[))]"
Set reg = CreateObject("vbscript.regexp")
reg.Global = True: reg.MultiLine = True
reg.Pattern = "" & r2 & "|" & r1 & "|" & r4 & "|" & r3 & ""
For Each mt In reg.Execute(ostr)
m = mt.FirstIndex: n = mt.Length
With ActiveDocument.Range(m, m + n)
If Not .Information(wdWithInTable) Then
.Expand 4: L = Len(.Text): .Collapse
If .MoveWhile(sr, L) > 0 Then
.Expand 4
.Style = "标题 2"
' .Font.ColorIndex = 6
ElseIf .MoveWhile("((", L) > 0 Then
If .MoveWhile(sr, L) > 0 Then
.Expand 4
.Style = "标题 3"
' .Font.ColorIndex = 5
Else
.Expand 4
.Style = "标题 5"
' .Font.ColorIndex = 12
End If
Else
.Expand 4
.Style = "标题 4"
' .Font.ColorIndex = 11
End If
End If
End With
Next
End Sub
Sub 删除空行()
Dim i As Paragraph
For Each i In ActiveDocument.Paragraphs
If Len(i.Range) = 1 And Asc(i.Range) = 13 Then i.Range.Delete
Next
End Sub
Sub 特殊样式()
With Selection
With .Font
.Size = 16
.Kerning = 0
.DisableCharacterSpaceGrid = True
End With
With .ParagraphFormat
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
.SpaceBefore = 0
.SpaceAfter = 0
.LineSpacing = LinesToPoints(1.25)
.CharacterUnitFirstLineIndent = 2
.AutoAdjustRightIndent = False
.DisableLineHeightGrid = True
.KeepWithNext = False
.KeepTogether = False
End With
End With
End Sub |
|