【问题标题】:How to use headers and page breaks properly while generating word?生成单词时如何正确使用页眉和分页符?
【发布时间】:2020-11-25 08:00:53
【问题描述】:

我很困惑如何正确使用页眉和分页符。我有使用 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

我尝试将 Type 更改为 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

它似乎无法检测到分页符。我到底做错了什么以及如何达到我想要的结果?

【问题讨论】:

  • 您没有插入分页符。在documentation 中查找分节符类型,您会看到已插入分节符。
  • 原始代码使用 wdPageBreak 或 7,是的,我使用了相同的文档,只是提到我也尝试了 2 和 4。
  • 在这个阶段不要使用 Createobject,使用对 Word 的引用,以便您可以访问 Word 对象的智能感知。 (Tools.References 向下滚动并确保选中 Microsoft Word Object XX.XX 旁边的复选框。)。您是否混淆了标题和标题?如果你确实想要一个 Header 那么这些是 Section 对象的一个​​属性。页面在 Word 中并不真正存在,它们是由打印机驱动程序生成的人工制品。使用部分和生活会容易得多。
  • 相反,如果您使用段落样式作为标题(如您所愿),并且您希望该标题在新页面上开始,请为该样式提供“分页符之前”属性) .这样你就不需要代码来插入分页符了。
  • 插入分页符本身不会导致文档中出现空白页。可以插入某些类型的分节符 wdSectionBreakEvenPage (4) 或 wdSectionBreakOddPage (5)。仅当您进行影响页面布局(包括页眉和页脚)的更改时才添加分节符。

标签: excel vba ms-word page-break


【解决方案1】:

以下代码使用 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

【讨论】:

  • 只有在您进行影响页面布局的更改时才应插入分节符。在任何情况下,都不应使用分节符来添加新页面。
  • @TimothyRylatt 如果不允许从新页面开始的新部分,分节符还有什么其他用途。手动分页符是魔鬼的工作,应该不惜一切代价避免。
  • @freeflow 分节符大大增加了文档的复杂性。手动分页符更好,但仍然不是那么好。开始新页面的最佳方法是在开始新页面的段落样式中设置分页符。分节符是开始新页面的一个非常糟糕的选择。这是macropod在对该问题的评论中提出的。 addbalance.com/word/MovePages.htm
  • 分节符应该在页面布局改变时使用,例如更改纸张大小、纵向到横向、页码格式、页眉或页脚更改。添加一个部分只是为了将文本强制到新页面上是一种非常糟糕的做法。 有效的情况是必须使用奇数页或偶数分页符以允许双面打印。
  • 不,他们不是。 标题样式用于组织内容,部分用于页面布局。
猜你喜欢
  • 2015-03-08
  • 2017-02-17
  • 2019-09-29
  • 2016-07-14
  • 2012-01-05
  • 2013-05-16
  • 2016-11-04
  • 2020-02-15
  • 2023-03-06
相关资源
最近更新 更多