最后一行

Sub AddPageBreakAfterLastOccurrencePerPage()
    Dim doc As Document
    Set doc = ActiveDocument

    Dim searchString As String
    searchString = String(36, "=") ' 创建一个包含36个等号的字符串

    Dim para As Paragraph
    Dim lastPara As Paragraph
    Dim currentPage As Integer
    Dim lastPage As Integer

    ' 初始化为文档的第一页
    currentPage = 1
    lastPage = doc.Range.Information(wdActiveEndAdjustedPageNumber)

    ' 遍历所有段落
    For Each para In doc.Paragraphs
        ' 检查当前段落所在的页码
        If para.Range.Information(wdActiveEndAdjustedPageNumber) = currentPage Then
            ' 如果段落包含36个等号,则记住这个段落
            If InStr(para.Range.Text, searchString) > 0 Then
                Set lastPara = para
            End If
        Else
            ' 当到达新一页时,如果上一页有包含36个等号的段落,就在其后插入分页符
            If Not lastPara Is Nothing Then
                lastPara.Range.InsertBreak Type:=wdPageBreak
                Set lastPara = Nothing ' 重置最后一个段落
            End If
            ' 更新当前页码
            currentPage = para.Range.Information(wdActiveEndAdjustedPageNumber)
            ' 如果当前段落包含36个等号,则记住这个段落
            If InStr(para.Range.Text, searchString) > 0 Then
                Set lastPara = para
            End If
        End If
    Next para

    ' 检查最后一页是否需要在最后一个段落后添加分页符
    If Not lastPara Is Nothing Then
        lastPara.Range.InsertBreak Type:=wdPageBreak
    End If
End Sub
此条目发表在日常分类目录。将固定链接加入收藏夹。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注