首页 > 解决方案 > MS Word VBA 链接多个部分和页面的上一个页脚

问题描述

我有一个 MS Word (2010),我的文档有数百个部分,而我真的只需要有限数量的具有相同页脚的部分。

我想要一个可以循环浏览所选页面并将所有页脚更改为链接到上一个的宏。

我录制了一个宏,它将为一个部分执行此操作,如何更改代码,以便如果我选择了一堆页面(例如 25 个),它将压缩所有页面?

Sub LinkToPrev_Foot()
'
' LinkToPrev_Foot Macro
'
'
    Selection.HeaderFooter.LinkToPrevious = Not Selection.HeaderFooter. _
        LinkToPrevious
   ' ActiveWindow.ActivePane.View.NextHeaderFooter
End Sub

标签: vbams-wordfooter

解决方案


您需要遍历文档的各个部分。下面的代码假定您只使用三种类型的页脚中的一种。

Public Sub LinkToPrev_Foot()
    Dim ftr As HeaderFooter
    Dim sec As Section

    For Each sec In ActiveDocument.Sections
        'can't link the first section to a previous one
        If sec.Index > 1 Then
            'if document has other types of footer you'll need to loop through them
            sec.Footers(wdHeaderFooterPrimary).LinkToPrevious = True
        End If
    Next sec
End Sub

推荐阅读