vba - splitting word document into sections: how to control update of page numbering
问题描述
I have a macro that cuts a document into sections of one page each:
Selection.HomeKey Unit:=wdStory
While Selection.Information(wdActiveEndPageNumber) < Selection.Information(wdNumberOfPagesInDocument)
ActiveDocument.Bookmarks("\page").Range.Select
With Selection.Find
.Text = "^b"
.Forward = True ' or False
.Wrap = wdFindStop
.Format = False
If .Execute Then
' found section break: go to next page
Selection.GoToNext wdGoToPage
Else
' found no section break: append one
Selection.Collapse Direction:=wdCollapseEnd
Selection.InsertBreak Type:=wdSectionBreakNextPage
End If
End With
Wend
I can re-run the macro after editing the document and only an extended page will be split again.
Following the above code I loop over all sections and disable the 'link to previous' property in the headers and footers. Then I loop over the sections again to 'unlink' the PAGE and NUMPAGE fields, that is, to replace the fields with their actual values.
This works for some documents and doesn't for others. In a problem document, when I enter a section break (manually or via VBA), the page number on the following section jumps to 1, while in a no-problem document it does not.
How do I control automatic page number updating when adding a section break?
解决方案
页码是否重新开始由页眉和页脚\页码\格式页码控制,设置“开始于”(与“从上一节继续”)。如果将其设置为数字,则在插入分节符时页面编号将重新开始。默认情况下,这是“关闭”,但它可能会在模板中打开,例如。
在对象模型中,等价的对象是Document.Section.HeaderFooter.PageNumbers
属性RestartNumberingAtSection
。将此设置为False
以使编号从一个部分连续到下一个部分。如果确定文档只有一个部分,则可以针对该部分执行此操作,并且任何新部分都将“继承”该设置。否则,在循环中检查它的同时SameAsPrevious
设置为False
。
Sub TestBreakUpPages()
Dim Doc As Word.Document
Dim Sec As Word.Section
Dim hdr As Word.HeaderFooter
Dim pageNum As PageNumbers
Set Doc = ActiveDocument
Selection.HomeKey Unit:=wdStory
While Selection.Information(wdActiveEndPageNumber) < Selection.Information(wdNumberOfPagesInDocument)
Doc.Bookmarks("\page").Range.Select
With Selection.Find
.Text = "^b"
.Forward = True ' or False
.wrap = wdFindStop
.Format = False
If .Execute Then
' found section break: go to next page
Selection.GoToNext wdGoToPage
Else
' found no section break: append one
Selection.Collapse Direction:=wdCollapseEnd
Selection.InsertBreak Type:=wdSectionBreakNextPage
End If
End With
Wend
For Each Sec In Doc.Sections
Set hdr = Sec.Headers(wdHeaderFooterPrimary)
Set pageNum = hdr.PageNumbers
If pageNum.RestartNumberingAtSection Then
pageNum.RestartNumberingAtSection = False
End If
hdr.LinkToPrevious = False
Next
For Each Sec In Doc.Sections
Set hdr = Sec.Headers(wdHeaderFooterPrimary)
hdr.Range.Fields.Unlink
Next
End Sub
推荐阅读
- reactjs - 使用 Strapi 和 React.js 发送邮件
- ios - PushKit 通知在通话结束后到达
- javascript - 从变量引用时,setAttribute 不是函数
- javascript - scope.data 没有 foreach 方法
- python - 如果它包含指定的短语,则删除整个 JSON 对象(来自 python 中的列表)
- php - 没有这样的文件或目录:我该如何解决这个问题?
- javascript - Firebase 云功能在部署时未更新 - 仍在服务旧版本的功能
- javascript - jQuery 函数仅在调试器模式下工作
- c++ - QT C++ OPENGL 和在其他机器上运行时的问题
- image - whatsapp 在哪里存储默认壁纸的图像?