如何使用VBA在我的Word模板的页脚中插入页码字段逻辑?

时间:2019-04-05 03:28:49

标签: vba ms-word word-vba footer page-numbering

我有一个单词模板,该模板根据部分使用不同的页脚字段。有时,此模板的用户会弄乱页脚,因此我正在编写宏以通过将默认页脚字段放回来修复页脚。
基于该部分,页脚字段中包含一些字段逻辑,基本上,我需要执行以下操作:

  1. 从第5节重新启动页码

  2. 根据以下各节,将文本插入第1行第2列的页脚中的表格中

第1至4节: {PAGE} //请注意,该格式为罗马数字格式,页脚设置为“首页不同”

第5节及以后 {if {page} <{= {pageref ReferencesEnd} + 1}“ {= {pageref ReferencesEnd}的页面{= {page}}“” {Styleref“ Att-Appendix Heading” \ n}“

我已经成功完成了第一步,并为第1至4节插入了字段,但是我在为如何使用以下方法以编程方式将第5+节的复杂字段逻辑插入模板中的相关页脚而苦恼VBA? 我需要的代码在下面的代码块中被注释为: “此处需要将下面的字段逻辑插入页脚中的代码

Sub FixPageNumbering()

    Dim intSect As Integer

   On Error Resume Next

    'Insert footer code for Sections 1-4 into row1,col1 of 2x2 table
    For intSect = 1 To 4

        With ActiveDocument.Sections(intSect).Footers(wdHeaderFooterPrimary)
            .PageNumbers.NumberStyle = wdPageNumberStyleLowercaseRoman
            .Range.Tables(1).Rows(1).Cells(2).Select
            Selection.TypeText Text:="Page "
            Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
            "PAGE ", PreserveFormatting:=True
        End With
    Next intSect

    'Set page numbering to restart at #1 from Section 5
    With ActiveDocument.Sections(5).Footers(wdHeaderFooterPrimary).PageNumbers
     .RestartNumberingAtSection = True
     .StartingNumber = 1
    End With

    'Insert footer code for Sections 5 and onwards into row1,col1 of 2x2 table
    For intSect = 5 To ActiveDocument.Sections.Count
        With ActiveDocument.Sections(intSect).Footers(wdHeaderFooterPrimary)
            .PageNumbers.NumberStyle = wdPageNumberStyleArabic
            .Range.Tables(1).Rows(1).Cells(2).Select

            'NEED CODE HERE TO INSERT THE FOLLOWING FIELD LOGIC INTO FOOTER
            '{ if { page } < { = { pageref ReferencesEnd } + 1 } "Page { = { page } } of { = { pageref ReferencesEnd }" "{Styleref "Att-Appendix Heading" \n }"

          End With

    Next intSect

    ActiveWindow.View.Type = wdPrintView


End Sub

对于第5节及以后的部分,页脚字段应显示&的第#页,或者当有附录(对于ReferencesEnd书签之后的页面)时,将显示“附录#”

2 个答案:

答案 0 :(得分:0)

尽管可以通过VBA创建复杂的字段结构,但最好将所需的字段代码存储在源文档的两个单独的段落中,您的宏可以在其中将它们复制并粘贴到目标中的适当位置文件。通过这种方法,您可以使用如下代码:

Sub Demo()
Application.ScreenUpdating = False
Dim DocSrc As Document, DocTgt As Document
Dim i As Long, Rng As Range, HdFt As HeaderFooter
Set DocSrc = ThisDocument
With Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
  .Title = "Select the target file"
  .AllowMultiSelect = False
  If .Show = -1 Then
    Set DocTgt = Documents.Open(.SelectedItems(1))
  Else
    MsgBox "No target file selected. Exiting", vbExclamation
    GoTo ErrExit
  End If
End With
With DocTgt
  For i = 1 To .Sections.Count
    Select Case i
      Case 1 To 4: Set Rng = DocSrc.Paragraphs(1).Range
      Case Else: Set Rng = DocSrc.Paragraphs(2).Range
    End Select
    With .Sections(i)
      For Each HdFt In .Footers
        With HdFt
          If .Exists Then
            If .LinkToPrevious = False Then
              .Range.FormattedText = Rng.FormattedText
              .Range.Characters.Last.Delete
            End If
          End If
        End With
      Next
    End With
  Next
End With
ErrExit:
Set Rng = Nothing: Set DocSrc = Nothing: Set DocTgt = Nothing
Application.ScreenUpdating = True
End Sub

如果有必要(虽然似乎不太可能),则可以用代码补充上述内容以应用所需的编号格式-或者可以将适当的开关添加到域代码本身。

PS:您的第二个域代码可以简化为-

{IF{PAGE}< {={PAGEREF ReferencesEnd}+1} "Page {PAGE} of {PAGEREF ReferencesEnd}" {STYLEREF "Att-Appendix Heading" \n}}

答案 1 :(得分:0)

如前所述,可以通过VBA创建复杂的字段结构。对于这种方法,您可以使用如下代码:

Sub Demo()
Application.ScreenUpdating = False
Dim DocTgt As Document, StrCode As String
Dim i As Long, Rng As Range, HdFt As HeaderFooter
With ActiveDocument
  For i = 1 To .Sections.Count
    Select Case i
      Case 1 To 4
        With .Sections(i)
          For Each HdFt In .Footers
            With HdFt
              If .Exists Then
                With .PageNumbers
                  .NumberStyle = wdPageNumberStyleLowercaseRoman
                  .RestartNumberingAtSection = False
                End With
                If .LinkToPrevious = False Or i = 1 Then
                  .Range.Fields.Add .Range, wdFieldEmpty, "PAGE", False
                End If
              End If
            End With
          Next
        End With
      Case Else:
        With .Sections(i)
          For Each HdFt In .Footers
            With HdFt
              If .Exists Then
                If i = 5 Then
                  .LinkToPrevious = False
                  With .PageNumbers
                    .NumberStyle = wdPageNumberStyleArabic
                    .RestartNumberingAtSection = True
                    .StartingNumber = 1
                  End With
                Else
                  With .PageNumbers
                    .NumberStyle = wdPageNumberStyleArabic
                    .RestartNumberingAtSection = False
                  End With
                End If
                If .LinkToPrevious = False Then
                  With .Range
                    .Fields.Add .Duplicate, wdFieldEmpty, "IF<  ""Page  of """, False
                    Set Rng = .Duplicate
                    With Rng
                      .Start = .Start + 19
                      .Collapse wdCollapseStart
                      .Fields.Add .Duplicate, wdFieldEmpty, "STYLEREF ""Att-Appendix Heading"" \n", False
                    End With
                    Set Rng = .Duplicate
                    With Rng
                      .Start = .Start + 17
                      .Collapse wdCollapseStart
                      .Fields.Add .Duplicate, wdFieldEmpty, "PAGEREF ReferencesEnd", False
                    End With
                    Set Rng = .Duplicate
                    With Rng
                      .Start = .Start + 13
                      .Collapse wdCollapseStart
                      .Fields.Add .Duplicate, wdFieldEmpty, "PAGE", False
                    End With
                    Set Rng = .Duplicate
                    With Rng
                      .Start = .Start + 6
                      .Collapse wdCollapseStart
                      .Fields.Add .Duplicate, wdFieldEmpty, "=+1", False
                      .Start = .Start + 3
                      .Collapse wdCollapseStart
                      .Fields.Add .Duplicate, wdFieldEmpty, "PAGEREF ReferencesEnd", False
                    End With
                    .Start = .Start + 4
                    .Collapse wdCollapseStart
                    .Fields.Add .Duplicate, wdFieldEmpty, "PAGE", False
                  End With
                End If
              End If
            End With
          Next
        End With
    End Select
  Next
End With
End Sub