|
应坛友的要求,对10楼的制作方法试用VBA方式进行处理。制作的基本思路及对文档的基本要求见代码说明。
Sub TOCwithAuthor()
'在光标处插入带作者的目录
'假设文档中每篇文章紧跟标题(有特定的大纲级别)之后的段落段内容均为作者名
Dim a As String, myRange As Range, StrTitle As String, StrAuthor As String, Para As Paragraph, st As Long
a = InputBox("请输入文章标题的大纲级别", , 1)
If Val(a) > 9 Or Val(a) < 1 Then Exit Sub
Application.ScreenUpdating = False
ActiveDocument.ActiveWindow.View.ShowFieldCodes = False
Set myRange = Selection.Range
With ActiveDocument.Content.Find
.ClearFormatting
.Format = True
.ParagraphFormat.OutlineLevel = Val(a)
Do While .Execute '依次插入目录项域
With .Parent
StrTitle = Replace(.Text, Chr(13), "")
StrTitle = Replace(StrTitle, Chr(12), "")
StrTitle = Replace(StrTitle, "“", "\“")
StrTitle = Replace(StrTitle, "”", "\”")
StrAuthor = Replace(.Next(wdParagraph), Chr(13), "")
If StrAuthor = "" Then StrAuthor = ChrW(8204)
ActiveDocument.Fields.Add ActiveDocument.Range(.Start, .Start), wdFieldEmpty, _
"TC """ & StrTitle & vbTab & StrAuthor & """ \f C \l ""1""", False
.SetRange .End, ActiveDocument.Content.End
End With
Loop
End With
If StrTitle = "" Then Exit Sub
Selection.Fields.Add Selection.Range, wdFieldEmpty, "TOC \f \h \z", False '插入基于目录项生成的目录
With myRange '临时处理生成的目录(更新目录即失效)
st = .Start
.End = Selection.End
.Font.Size = .Characters.Last.Font.Size
If .Fields.Count > 0 Then .Fields.Locked = True '锁定目录域(防止更新目录域)
For Each Para In .Paragraphs '设置目录条目中作者名右对齐位置及前导符
' Para.Format.TabStops(1).Position = Para.Format.TabStops(1).Position - Selection.Font.Size / 2 '用于调整作者名右对齐位置
Para.Format.TabStops(1).Leader = wdTabLeaderMiddleDot
Next
Do While .Find.Execute("^t^#") '页码前插入括弧
.MoveStart
.InsertBefore "("
.SetRange .End, Selection.Range.End
Loop
.SetRange st, Selection.End
.Find.Execute "^p", replacewith:=")^&", Replace:=wdReplaceAll '页码后插入括弧
End With
Application.ScreenUpdating = True
End Sub |
|