|
自动排版标题居中就好了,文章后的落款和时间却要置于右侧,算法是个问题,实现起来也问题多多。代码如下,求指教。
Sub 排版文章结尾的落款和时间()
'排版文章结尾的落款和时间
'调整落款和时间位置,分别可能有以下情况:1、有落款和时间。2、只有时间无落款。3、落款为xxx:,如合同等待签字类型。4、落款很长或几个单位
Dim ew As Range, ew2 As Range, ew3 As Range, ha As Integer, i As Integer, p As Integer
'删除文章后空行和空格段,以及倒数第二段空段落
Do While ActiveDocument.Paragraphs.Count > 1
Set ew = ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count).Range
If Len(Replace(ew.Text, " ", "")) = 1 Then
ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count).Range.Delete
Else
Exit Do
End If
Loop
If Not (ew.Text Like "*年*月*" & Chr(13)) Then GoTo voa: '如果最后一段非时间格式则倒数1-2段不必排版,
'至此,已经明确最后一段时间,再检查倒数2段
Do While ActiveDocument.Paragraphs.Count > 2
Set ew2 = ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count - 1).Range
If Len(Replace(ew2.Text, " ", "")) = 1 Then
ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count - 1).Range.Delete
Else
Exit Do
End If
Loop
'删除文章后空行和空格段,以及倒数第二段空段落
'取最大行字符数,最多遍历15行,还有更好的办法吗?页面设置ActiveDocument.PageSetup.CharsLine不可用
Selection.EndKey unit:=wdStory
y = Selection.Information(wdFirstCharacterLineNumber)
Selection.HomeKey unit:=wdStory
Do Until i = 15 Or i = y - 2
Selection.MoveDown unit:=wdLine, Count:=1
Selection.HomeKey unit:=wdLine
Selection.EndKey unit:=wdLine, Extend:=wdExtend
i = i + 1
p = Len(Selection.Range.Text)
If p > ha Then ha = p
Loop
'取最大行字符数ha
With ActiveDocument
If (Len(ew2.Text) < (ha * 3 / 4)) And (ew.Information(wdFirstCharacterLineNumber) - ew2.Information(wdFirstCharacterLineNumber) = 1) Then
'落款时间都有
'将倒数第三行空行删除
Do While ActiveDocument.Paragraphs.Count > 3
Set ew3 = ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count - 2).Range
If Len(Replace(ew3.Text, " ", "")) = 1 Then
ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count - 2).Range.Delete
Else
Exit Do
End If
Loop '将倒数第三行空行删除
Set ew = ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count).Range
ew.ParagraphFormat.Alignment = wdAlignParagraphLeft
ew.Text = Replace(LTrim(RTrim((ew.Text))), Chr(13), "") '去掉左右侧可能空格,落款可能是多个主体,故中间空格保留
If Int(Len(ew.Text) / 2) < Round(ha / 4 + 0.4, 0) Then
For i = 1 To Len(Space(ha - (Len(ew.Text) - (Round(ha / 4, 0) - Int(Len(ew.Text) / 2)))))
ActiveDocument.Range(ew.Start, ew.Start).InsertBefore " "
Next
End If
Set ew2 = ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count - 1).Range
ew2.ParagraphFormat.Alignment = wdAlignParagraphLeft
ew2.Text = Replace(LTrim(RTrim((ew2.Text))), Chr(13), "") '去掉左右侧可能空格,落款可能是多个主体,故中间空格保留
If Int(Len(ew2.Text) / 2) < Round(ha / 4 + 0.4, 0) Then
For i = 1 To Len(Space(ha - (Len(ew2.Text) - (Round(ha / 4, 0) - Int(Len(ew2.Text) / 2)))))
ActiveDocument.Range(ew2.Start, ew2.Start).InsertBefore " "
Next
End If
ew2.InsertBefore Chr(13)
ew2.InsertBefore Chr(13)
ElseIf (Len(ew2.Text) >= ha * 3 / 4) Or (ew.Information(wdFirstCharacterLineNumber) - ew2.Information(wdFirstCharacterLineNumber) <> 1) Then
'落款太长不符合,仅仅排时间
ew.Text = LTrim(RTrim(Replace((ew.Text), Chr(13), ""))) & Chr(13) '去掉右侧可能空格
ew.ParagraphFormat.Alignment = wdAlignParagraphLeft
If Int(Len(Trim(ew)) / 2) < 7 Then
For i = 1 To 8 - Int(Len(LTrim(RTrim(Replace((ew.Text), Chr(13), "")))) / 2)
ActiveDocument.Range(ew.Start, ew.End - 1).InsertAfter " "
Next
ew.InsertBefore Chr(13)
ew.InsertBefore Chr(13)
End If
End If '都不满足条件,不需要排、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
'落款为xxx:待签字类型
voa:
Set ew = Nothing
Set ew2 = Nothing
Set ew3 = Nothing
End With
End Sub
|
|