使用 vba 插入封面

问题描述 投票:0回答:1

我正在尝试开发 vba,它可以插入一个封面页,该封面页与在 UI 中使用“插入封面页”命令时插入的封面页相同。

在当前 Windows 版本的 Word(2016+)中,此

  • 插入用户选择的封面构建块
  • 将页码设置为 0。此页不计入 Page 或 NumPages 字段,但计入 Pages 文档属性
  • 激活首页页眉
  • 封面页内容后面有分页符(不是分节符)
  • 如屏幕截图所示,分页有点奇怪。请注意字段和状态栏。

enter image description here

我能想到的最好的方法是以下使用分节符的 vba。该页面将计入 NumPages 字段以及 Pages 文档属性中。 我在《章节作为封面页异常》一章的写作中探讨了这一点。

Selection.HomeKey Unit:=wdStory
Selection.InsertBreak Type:=wdSectionBreakNextPage
Selection.EndKey Unit:=wdStory
Selection.HomeKey Unit:=wdStory
With Selection.Sections(1).Headers(1).PageNumbers
    .NumberStyle = wdPageNumberStyleArabic
    .HeadingLevelForChapter = 0
    .IncludeChapterNumber = False
    .ChapterPageSeparator = wdSeparatorHyphen
    .RestartNumberingAtSection = True
    .StartingNumber = 0
End With
With ActiveDocument
    .Sections(2).Headers(wdHeaderFooterFirstPage).LinkToPrevious = False
    .Sections(2).Footers(wdHeaderFooterFirstPage).LinkToPrevious = False
    .Sections(1).PageSetup.DifferentFirstPageHeaderFooter = True
    .Sections(1).Headers(wdHeaderFooterFirstPage).Range.Text = ""
    .Sections(1).Footers(wdHeaderFooterFirstPage).Range.Text = ""
End With

我不知道有任何 Word 命令或 vba 命令与用户界面命令相对应。有没有办法使用 vba 插入封面页,其结果与在 UI 中使用命令的结果相同?

vba ms-word
1个回答
0
投票

您可以尝试以下操作作为起点,假设您要插入名为“Whisp”的内置封面页

Sub InsertWhispCoverPage()
Dim bb As Word.BuildingBlock
Dim d As Word.Document
Set d = ActiveDocument
Set bb = getCPBBOrNothing("Whisp")
If bb Is Nothing Then
  Debug.Print "Could not find the specified cover page"
Else
  ' Perhaps need to deal with things at the beginning of the doc. that cannot be overwritten
  d.Range(0, 0).InsertBreak Word.WdBreakType.wdPageBreak
  bb.Insert where:=d.Range(0, 0), RichText:=True
End If
Set d = Nothing
Set bb = Nothing
End Sub

Function getCPBBOrNothing(BBName As String) As BuildingBlock
Dim i As Long
Dim j As Long
Dim k As Long
With Application.Templates
  .LoadBuildingBlocks
  For i = 1 To .Count
    With .Item(i).BuildingBlockTypes(wdTypeCoverPage).Categories
      For j = 1 To .Count
        With .Item(j).BuildingBlocks
          For k = 1 To .Count
            If .Item(k).Name = "Whisp" Then
              Debug.Print .Item(k).Category.Name, .Item(k).Type.Name
              Set getCPBBOrNothing = .Item(k)
              Exit Function
            End If
          Next
        End With
      Next
    End With
  Next
End With
End Function

这提出了几个问题,例如“您实际上如何决定使用哪个封面页”,以及“您实际上是否尝试在不使用封面页的情况下做到这一点?”。我确信这些构建模块可以比我更好地处理。

如果您试图避免使用封面构建块,那么问题在于,正如 @TimothyRylatt 提到的,它是触发页码行为的构建块。我查看了插入的 OOXML,文档部分开始了这样的事情

<w:body>
  <w:sdt>
    <w:sdtPr>
      <w:id w:val="1382976450"/>
      <w:docPartObj>
        <w:docPartGallery w:val="Cover Pages"/>
        <w:docPartUnique/>
      </w:docPartObj>
    </w:sdtPr>
    <w:sdtContent>

后面是 SDT 的内容,然后以

结束
    </w:sdtContent>
  </w:sdt>

最终

</w:body>

可能能够通过使用Range.InsertXML插入代码来模拟在开始时拥有合适的构建块。我还没有时间尝试,但如果可能的话,我认为你最需要的是 <w:sdt> <w:sdtPr> <w:docPartObj> <w:docPartGallery w:val="Cover Pages"/> <w:docPartUnique/> </w:docPartObj> </w:sdtPr> </w:sdt>

您必须在 
w:val

中具有

DocPartGalleryElement
属性,并且必须将其设置为
"Cover Pages"
    

© www.soinside.com 2019 - 2024. All rights reserved.