首页 > 解决方案 > 生成单词时如何正确使用页眉和分页符?

问题描述

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

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

标签: excelvbams-wordpage-break

解决方案


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

推荐阅读