将word文档分成几个部分:如何控制页码的更新
splitting word document into sections: how to control update of page numbering
我有一个宏可以将文档分成一页的部分:
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
我可以在编辑文档后重新运行宏,只有扩展页面会再次拆分。
按照上面的代码,我遍历所有部分并禁用页眉和页脚中的 'link to previous' 属性。然后我再次遍历这些部分以 'unlink' PAGE 和 NUMPAGE 字段,也就是说,用它们的实际值替换这些字段。
这对某些文档有效,对其他文档无效。在有问题的文档中,当我输入分节符(手动或通过 VBA)时,下一节的页码跳转到 1,而在没有问题的文档中则不会。
添加分节符时如何控制自动页码更新?
页码是否重新开始由页眉和Footer\Page Number\Format页码控制,设置"Start at"(对比"Continue from previous section")。如果将其设置为数字,则在插入分节符时将重新开始页码编号。默认情况下,这是 "off",但它可能会在模板中打开,例如。
在对象模型中等价对象是Document.Section.HeaderFooter.PageNumbers
、属性RestartNumberingAtSection
。将此设置为 False
以使编号从一个部分到下一个部分是连续的。如果确定文档只有一个部分,则可以为该部分完成此操作,任何新部分都将 "inherit" 设置。否则在循环中同时检查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
我有一个宏可以将文档分成一页的部分:
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
我可以在编辑文档后重新运行宏,只有扩展页面会再次拆分。
按照上面的代码,我遍历所有部分并禁用页眉和页脚中的 'link to previous' 属性。然后我再次遍历这些部分以 'unlink' PAGE 和 NUMPAGE 字段,也就是说,用它们的实际值替换这些字段。
这对某些文档有效,对其他文档无效。在有问题的文档中,当我输入分节符(手动或通过 VBA)时,下一节的页码跳转到 1,而在没有问题的文档中则不会。
添加分节符时如何控制自动页码更新?
页码是否重新开始由页眉和Footer\Page Number\Format页码控制,设置"Start at"(对比"Continue from previous section")。如果将其设置为数字,则在插入分节符时将重新开始页码编号。默认情况下,这是 "off",但它可能会在模板中打开,例如。
在对象模型中等价对象是Document.Section.HeaderFooter.PageNumbers
、属性RestartNumberingAtSection
。将此设置为 False
以使编号从一个部分到下一个部分是连续的。如果确定文档只有一个部分,则可以为该部分完成此操作,任何新部分都将 "inherit" 设置。否则在循环中同时检查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