【发布时间】: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
这两张照片是我希望最终产品的样子。
【问题讨论】:
-
如果页眉中出现的文本作为标题出现在文档正文中,您只需要一个包含 StyleRef 字段的页眉 - 请参阅wordfaqs.ssbarnhill.com/StyleRef.htm。如果你真的需要一个新的部分,你需要添加一个分节符——见learn.microsoft.com/en-us/office/vba/api/word.range.insertbreak和learn.microsoft.com/en-us/office/vba/api/word.wdbreaktype