|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
可试试如下代码,其实是依据手工编写的域代码再编写宏代码的,插入嵌套域操作比较繁琐。- Sub test2()
- '同时显示节内页码和文档总页码,仅适用于每节均重新起始页码时
- Dim i%, n%, aStart0&, codetext1$, codetext2$
- Dim aCharnum, codetext0, aStart1, aStart2, aEnd1, aEnd2
- Dim aSec As Section
- Dim aFooterRange As Range
- Dim aField As Field
-
- aCharnum = Array(17, 13, 8, 4)
- codetext0 = Array("numpages", "quote", "sectionpages", "page")
- aStart1 = Array(63, -13, -4, 12)
- aEnd1 = Array(75, -1, 28, 19)
- aStart2 = Array(119, -27, 3, 6, 15, -40, -17, 3, 6, 3, -29, 10)
- aEnd2 = Array(123, 6, 15, 13, 27, -28, 16, 18, 16, 10, 37, 17)
- codetext1 = "set ""secsection"" sectionpages"
- codetext2 = "set ""secsection"" =""sec=section-1""+sectionpages=""secsection""-sectionpages+page"
-
- Application.ScreenUpdating = True
- ActiveWindow.View.ShowFieldCodes = True
- ActiveDocument.ActiveWindow.View.Type = wdPrintPreview
- ActiveDocument.ActiveWindow.View.SeekView = wdSeekPrimaryFooter
- For Each aSec In ActiveDocument.Sections
- i = i + 1
- aSec.Footers(wdHeaderFooterPrimary).LinkToPrevious = False
- With aSec.Footers(wdHeaderFooterPrimary).Range
- .Text = "节内第x页,共y页。总第m页,共n页"
- For n = 0 To 3
- .Fields.Add .Characters(aCharnum(n)), , codetext0(n), False
- Next
- aStart0 = .Fields(3).Code.Start + 7
-
- If i = 1 Then
- .Fields(3).Code.InsertAfter codetext1
- For n = 0 To UBound(aStart1)
- .SetRange .Start + aStart1(n), .Start + aEnd1(n)
- .Select
- Application.Run "InsertFieldChars"
- Next
- .MoveUntil "页"
- .Fields.Add .Duplicate, wdFieldPage, , False
- ElseIf i = 2 Then
- .Fields(3).Code.InsertAfter codetext2
- For n = 0 To UBound(aStart2)
- .SetRange .Start + aStart2(n), .Start + aEnd2(n)
- .Select
- Application.Run "InsertFieldChars"
- Next
- .Expand wdStory
- For Each aField In .Fields
- aField.Code.Characters.First.Delete
- If aField.Code.Characters.Last = Chr(32) Then aField.Code.Characters.Last.Delete
- Next
- Set aFooterRange = .FormattedText
- Else
- .FormattedText = aFooterRange
- End If
- .ParagraphFormat.Alignment = wdAlignParagraphCenter
- End With
- Next
- ActiveWindow.View.ShowFieldCodes = False
- ActiveDocument.ActiveWindow.View.SeekView = wdSeekMainDocument
- Application.ScreenUpdating = True
- MsgBox "处理完毕!", vbInformation
- End Sub
复制代码
se兄前面的文件中因宏太多,总体思路也不明,实在看不过来,十分抱歉。我觉得,如果主要是为了处理机关公文文档,可自行制作一个专用模板,再在模板中处理。有了模板,编辑公文文档时需要临时调整的事项就不会特别多。我平时不是专门做文档处理的,编程只是业余爱好,所以我的WORD编辑界面只添加了几个内置按钮和几个常用的自定义宏按钮,其余基本不动,很少用自定义快捷键或操作自定义功能区。
Range的Duplicate属性返回一个只读的Range对象,是原Range对象的独立复本。另外,当在With语句中要引用父对象Range时,也可用Duplicate属性代替,进行二次操作时其边界可独立引用。 |
评分
-
1
查看全部评分
-
|