|
本帖最后由 120332986 于 2022-3-3 11:26 编辑
文档已经合并好,现在需要添加目录。
1.新增一个页面
2.在新增页面上添加目录,目录不设页码,原合并文档的页码还是从第一页开始
3.自己写了一段vba ,但还是不行,目录不能添加到新增的页面,起始页码也不是从合并文档的第一页开始
Sub list()
'目录
Application.ScreenUpdating = False
' 目录1的样式设置
With ListGalleries(wdNumberGallery).ListTemplates(1).ListLevels(1)
.NumberFormat = "%1."
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleArabic
.NumberPosition = CentimetersToPoints(0)
.Alignment = wdListLevelAlignLeft
.TextPosition = CentimetersToPoints(1)
.TabPosition = CentimetersToPoints(1)
.ResetOnHigher = 0
.StartAt = 1
With .Font
.Bold = wdUndefined
.Italic = wdUndefined
.StrikeThrough = wdUndefined
.Subscript = wdUndefined
.Superscript = wdUndefined
.Shadow = wdUndefined
.Outline = wdUndefined
.Emboss = wdUndefined
.Engrave = wdUndefined
.AllCaps = wdUndefined
.Hidden = wdUndefined
.Underline = wdUndefined
.Color = wdUndefined
.Size = wdUndefined
.Animation = wdUndefined
.DoubleStrikeThrough = wdUndefined
.Name = "Times New Roman"
End With
.LinkedStyle = "目录 1"
End With
'如果已经有目录,删除,没有写判断循环语句,只删除1个目录
If ActiveDocument.TablesOfContents.Count > 0 Then
ActiveDocument.TablesOfContents(1).Delete
End If
'新建一个目录
ActiveDocument.Range(0).Select
Word.Selection.InsertNewPage
End With
With .Paragraphs(1).Range
With .Font
.Name = "方正黑体_GBK"
.Size = 22
End With
End With
End With
Set rng = ActiveDocument.TablesOfContents.Add(Range:=Selection.Range, RightAlignPageNumbers:= _
True, UseHeadingStyles:=True, UpperHeadingLevel:=1, _
LowerHeadingLevel:=1, IncludePageNumbers:=True, AddedStyles:="", _
UseHyperlinks:=True, HidePageNumbersInWeb:=True, UseOutlineLevels:= _
True)
'设置目录字体
With rng.Range.Font
.Size = 16
.Name = "方正仿宋_GBK"
End With
Selection.Previous.Select
'设置制表位
Selection.ParagraphFormat.TabStops.ClearAll
ActiveDocument.DefaultTabStop = CentimetersToPoints(0.74) '目录的默认2字符
Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(3.54), Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces '目录的右边距(A4页宽21cm-左边距2.8cm-2个字符0.74cm)目录编号是2位阿拉伯数字,占2字符
Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(15.6), Alignment:=wdAlignTabRight, Leader:=wdTabLeaderDots '目录的右边距(A4页宽21cm-左边距2.8cm-右边距2.6cm)
'给页码加()
With Selection.Find
.Text = "([0-9]{1,})"
.MatchWildcards = 1
Do While .Execute
If Selection.End > st Then Exit Do
Debug.Print Asc(.Parent)
If Asc(.Parent.Next) = 13 Then .Parent = "(" & .Parent & ")": st = st + 2
.Parent.Collapse wdCollapseEnd
Loop
End With
st = Selection.End
Application.ScreenUpdating = True
End Sub
|
|