|
运行本程序,必须保证“节”>2,程序的结果为:在页眉处分2行显示,总第?页共?页,第?节本节第?页本节共?页。- Sub AllPages()
- Dim i As Section, myRange As Range, myBk As Bookmark, strBk As String
- Dim KeyRange As Range, BtLenth As Byte
- On Error Resume Next
- With ActiveDocument
- If .Sections.Count = 1 Then Exit Sub
- Application.ScreenUpdating = False
- For Each myBk In .Bookmarks
- If VBA.InStr(myBk.Name, "myBK_S") = 1 Then myBk.Delete
- Next
- For Each i In .Sections
- Set myRange = .Range(i.Range.Start, i.Range.Start)
- strBk = "myBK_S" & i.Index
- BtLenth = Len(strBk)
- .Bookmarks.Add strBk, myRange
- With i.Headers(wdHeaderFooterPrimary)
- Set KeyRange = .Range
- .LinkToPrevious = False
- .Range.Text = "总第PAGE页共NUMPAGES页" & Chr(13) & "第SECTION节 ,本节第=PAGE-PAGEREF " & strBk & "+1页, 本节共SECTIONPAGES页"
- Set KeyRange = .Range
- KeyRange.SetRange KeyRange.Start + 38, KeyRange.Start + 38 + 8 + BtLenth
- KeyRange.Fields.Add KeyRange, wdFieldEmpty, , False
- Set KeyRange = .Range
- KeyRange.SetRange KeyRange.Start + 33, KeyRange.Start + 37
- KeyRange.Fields.Add KeyRange, wdFieldEmpty, , False
- Set KeyRange = .Range
- KeyRange.SetRange KeyRange.Start + 32, KeyRange.End - 20
- KeyRange.Fields.Add KeyRange, wdFieldEmpty, , False
- Set KeyRange = .Range
- KeyRange.SetRange KeyRange.End - 14, KeyRange.End - 2
- KeyRange.Fields.Add KeyRange, wdFieldEmpty, , False
- Set KeyRange = .Range
- KeyRange.SetRange KeyRange.Start + 19, KeyRange.Start + 26
- KeyRange.Fields.Add KeyRange, wdFieldEmpty, , False
- Set KeyRange = .Range
- KeyRange.SetRange KeyRange.Start + 8, KeyRange.Start + 16
- KeyRange.Fields.Add KeyRange, wdFieldEmpty, , False
- Set KeyRange = .Range
- KeyRange.SetRange KeyRange.Start + 2, KeyRange.Start + 6
- KeyRange.Fields.Add KeyRange, wdFieldEmpty, , False
- .Range.Fields.Update
- End With
- Next
- End With
- Application.ScreenUpdating = True
- End Sub
复制代码 虽然使用程序比域多输入多少字符,但是能方便修改,具有可读性。 |
|