excel - 生成单词时如何正确使用页眉和分页符?
问题描述
我很困惑如何正确使用标题和分页符。我有使用 Excel 中的 VBA 生成的 word 格式的报告。基本结构应该是这样的:
第 1 页
- 标题
- 文本
- 文本
- 分页符
第2页
- 文本
- 文本
- 文本
- 文本
我得到的结果是这样的:
第 1 页
- 标题
- 文本
- 文本
第2页
- 分页符
- 空白页
- 空白页
- 空白页
第 3 页
- 文本
- 文本
- 文本
- 文本
分页符是使用这部分代码创建的:
'Launches word application
Set Wapp = CreateObject("Word.Application")
With Wapp
With .Selection
'New paragraph & line
.TypeParagraph
.TypeText St1
.TypeText St2
'Collapses selection
.Collapse Direction:=0 'Direction:=wdCollapseEnd
'New page
.InsertBreak Type:=7 'Type:=wdPageBreak
End With
End With
我试图将类型更改为 4 或 2,然后它不会创建额外的页面,但随后标题出现在第二页中,我不希望这样。我也尝试在创建标题时添加此行,但它似乎不起作用:
With Doc.PageSetup
.DifferentFirstPageHeaderFooter = True
End With
然后我在想也许可以通过检测带有分页符的段落来删除空白页面并编写了以下代码:
'Procedure will check if there are blank pages and remove them
Sub DeleteLastPageBreak(Wapp As Object)
Dim i As Long
Dim Doc As Word.Document
Dim P As Paragraph
'Creates reference to Word document
Set Doc = Wapp.ActiveDocument
For Each P In Doc.Content.Paragraphs
If P.Range.Characters.Count = 1 Then
If P.Range.Characters(1) = vbFormFeed Then
MsgBox "paragraph found"
End If
End If
Next
End Sub
它似乎无法检测到分页符。我到底做错了什么以及如何达到我想要的结果?
解决方案
以下代码使用 Word 引用(早期绑定的 Word 对象)可能有助于说明原理。
Option Explicit
Sub main()
Dim myWord As Word.Application
Set myWord = New Word.Application
myWord.Visible = True
myWord.Activate
Dim myDoc As Word.Document
Set myDoc = myWord.Documents.Add
'Information for first section
With myDoc.StoryRanges(wdMainTextStory)
' At this point there is only 1 section in the document
With .Sections.Last
With .Headers(wdHeaderFooterPrimary).Range
.Paragraphs.Item(1).Range.Text = "This is paragraph1 in the header"
End With
With .Range.Paragraphs.Item(1).Range
.Style = myDoc.Styles(WdBuiltinStyle.wdStyleHeading1)
.Text = "MyHeadingText "
.InsertParagraphAfter
.Collapse Direction:=wdCollapseEnd
.Text = "Paragraph 2"
.InsertParagraphAfter
.Collapse Direction:=wdCollapseEnd
.Text = "paragraph 3"
.Collapse Direction:=wdCollapseEnd
'start a new section with next page enabled
.InsertBreak Type:=WdBreakType.wdSectionBreakNextPage
.Collapse Direction:=wdCollapseEnd
With .Sections.Item(1).Headers(wdHeaderFooterPrimary)
.LinkToPrevious = False
.Range.Paragraphs.Last.Range.Text = "This is header in section 2 which is on a new page"
End With
.Style = myDoc.Styles(WdBuiltinStyle.wdStyleHeading1)
.Text = "MyHeadingText in section 2 (on a new page)"
.InsertParagraphAfter
.Collapse Direction:=wdCollapseEnd
.Text = "Paragraph 2"
.InsertParagraphAfter
.Collapse Direction:=wdCollapseEnd
.Text = "paragraph 3"
.Collapse Direction:=wdCollapseEnd
End With
End With
End With
End Sub
推荐阅读
- azure - 使用 KeyVault 机密通过 PowerShell 部署 VM,无需明文转换
- flutter - 提示文本(应用启动时的帮助文本)颤动
- javascript - 如何使用特定的 json 数据在 d3.js 中创建多色垂直条形图?
- sql - Oracle - 计算两个日期之间的天数
- tensorflow - ModuleNotFoundError:没有名为“tf_explain”的模块
- mysql - 经度和纬度如何正确映射到弹簧数据点的x和y参数?
- sql - SQLite 删除很慢
- openmodelica - 使用带有现有类的图表视图在 OpenModelica 中创建包
- javascript - 如何在 OpenLayers 6.4.2 中禁用地图上的点击事件或绘制对象?
- postgresql - 如何使用现有数据推送我的 docker postgres 图像?