【问题标题】:Different Headers but Same Footer for Each Section每个部分的页眉不同但页脚相同
【发布时间】:2022-11-29 05:29:00
【问题描述】:

我将如何创建一个包含两个部分的文档,其中页眉不同但页脚相同?例如,我会将第一部分标记为“页眉 1”,将第二部分标记为“页眉 2”,但这两部分的页脚将相同,在本例中为“作者:John Apples”。

我在 MS Excel 中工作,因为我想从工作表中导入一些数据。

注意:我对 VBA 很陌生

我尝试使用“DifferentHeaderFirstPageHeaderFooter = True”,但这适用于页眉和页脚,而不仅仅是页眉。此外,我不认为我正在创建单独的部分,因此实现该功能会很好,因为我计划添加更多具有不同标题的部分。任何帮助将不胜感激。

我的代码

'Create a new Doc
Set myDocument = WordApp.Documents.Add
WordApp.Visible = True
WordApp.Activate

'Set Landscape Orientation
myDocument.PageSetup.Orientation = 1

'Set Margins
myDocument.PageSetup.BottomMargin = 26
myDocument.PageSetup.TopMargin = 26
myDocument.PageSetup.LeftMargin = 36
myDocument.PageSetup.RightMargin = 36

myDocument.Styles("Footer").Font.Size = 9
myDocument.Styles("Header").Font.Size = 18
myDocument.Styles("Header").Font.Color = RGB(0, 98, 155)

Set objSelection = WordApp.Selection

'Creating the header
objSelection.Sections(1).Headers(wdHeaderFooterPrimary).Range.InsertBefore "Header 1"
objSelection.Sections(1).Headers(wdHeaderFooterFirstPage).Range.InsertBefore "Header 2"

'Add Footer and Page Numbers
objSelection.Sections(1).Footers(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.InsertBefore "Author: John Apples"
objSelection.Sections(1).Footers(WdHeaderFooterIndex.wdHeaderFooterFirstPage).Range.InsertBefore "Author: John Apples"
        
objSelection.Sections(1).Footers(wdHeaderFooterFirstPage).PageNumbers.ShowFirstPageNumber = True
objSelection.Sections(1).Footers(wdHeaderFooterPrimary).PageNumbers.Add FirstPage:=True

        
objSelection.Font.Name = "Arial"
objSelection.Font.Size = 12
objSelection.Font.Color = RGB(0, 98, 155)
        
myDocument.Sections.First.PageSetup.DifferentFirstPageHeaderFooter = True

更新:

https://i.stack.imgur.com/Ko9rd.png

https://i.stack.imgur.com/GWPPu.png

这两张照片是我希望最终产品的样子。

【问题讨论】:

标签: excel vba ms-word


【解决方案1】:

您可以通过为文档提供“不同的第一页”布局(正如您所做的那样)来非常简单地做到这一点,使用:

Set myDocument = WordApp.Documents.Add
With myDocument
  'Set Landscape Orientation
  .PageSetup.Orientation = wdOrientLandscape
  'Set Margins
  .PageSetup.BottomMargin = 26
  .PageSetup.TopMargin = 26
  .PageSetup.LeftMargin = 36
  .PageSetup.RightMargin = 36
  'Set Style Fonts
  With .Styles("Normal").Font
    .Name = "Arial"
    .Size = 12
    .Color = RGB(0, 98, 155)
  End With
  .Styles("Footer").Font.Size = 9
  With .Styles("Header").Font
    .Size = 18
    .Color = RGB(0, 98, 155)
  End With
  'Create the layout
  With .Sections.First
    .PageSetup.DifferentFirstPageHeaderFooter = True
    'Creating the header
    With .Headers(wdHeaderFooterPrimary).Range
      .Style = "Header"
      .Text = "Header 1"
    End With
    .Headers(wdHeaderFooterFirstPage).Range.InsertBefore "Header 2"
   'Add Footer and Page Numbers
    With .Footers(wdHeaderFooterFirstPage)
      With .Range
        .Style = "Footer"
        .Text = "Author: John Apples"
      End With
    End With
    With .Footers(wdHeaderFooterPrimary)
      With .Range
        .Style = "Footer"
        .Text = "Author: John Apples"
      End With
      .PageNumbers.Add FirstPage:=False
    End With
  End With
End With

请注意,文档不需要可见或处于活动状态即可发生任何这些,并且您不需要选择任何内容。

然后,要添加具有不同页脚的新部分,您可以在最后的“结束于”之前插入以下代码。主页面标题将转移到新部分,并且通过取消新部分与前一个部分的链接,您可以更新新部分的页脚。

  'Add New Section with its own Footer and Page Numbers
  .Range.Characters.Last.InsertBreak Type:=wdSectionBreakNextPage
  With .Sections.Last
    With .Footers(wdHeaderFooterPrimary)
      .LinkToPrevious = False
      .Range.Text = "Author: Betty Blue"
      .PageNumbers.Add FirstPage:=False
    End With
    .PageSetup.DifferentFirstPageHeaderFooter = False
  End With

相反,要添加具有不同标题的新部分,您可以在最后的“结束于”之前插入以下代码。主页页脚将转移到新部分,并且通过取消新部分与前一个部分的链接,您可以更新新部分的页眉。

  'Add New Section with its own Header
  .Range.Characters.Last.InsertBreak Type:=wdSectionBreakNextPage
  With .Sections.Last
    With .Headers(wdHeaderFooterPrimary)
      .LinkToPrevious = False
      .Range.Text = "Header 3"
    End With
    .PageSetup.DifferentFirstPageHeaderFooter = False
  End With

【讨论】:

  • 感谢您的解决方案,它似乎帮助我理解了它应该如何构建,但仍然没有给我想要的输出。我已经更新了我的问题以包含我正在寻找的内容的屏幕截图。
  • 我的回答中的第一个宏按照您的描述和您的屏幕截图描述的那样执行。第二个代码块显示了如何添加一个具有相同页眉但页脚不同的新部分。第三个代码块(我现在添加的)显示了如何添加一个具有不同页眉但相同页脚的新部分。
猜你喜欢
  • 1970-01-01
  • 2015-09-29
  • 2018-10-10
  • 2012-01-02
  • 1970-01-01
  • 2014-10-13
  • 1970-01-01
  • 1970-01-01
  • 2016-04-29
相关资源
最近更新 更多