【问题标题】:Add a new page after all pages that contains specific text in Word document在 Word 文档中包含特定文本的所有页面之后添加新页面
【发布时间】:2019-10-31 19:15:54
【问题描述】:

我需要在所有包含特定单词(如“S U M M A R Y”)的页面之后添加一个空白页面。

Sub SelFind()
    Dim oRng As Range
    Set oRng = ActiveDocument.Range
    With oRng.Find
        .Text = "S U M M A R Y             "
        Selection.GoTo What:=wdGoToBookmark, Name:="\Page"
        Selection.MoveRight Unit:=wdCharacter, Count:=1
        Selection.MoveLeft Unit:=wdCharacter, Count:=1
        Selection.InsertBreak Type:=wdPageBreak
    End With
lbl_Exit:
    Exit Sub
End Sub

这是针对单个页面进行的。如何循环浏览所有页面。

【问题讨论】:

  • 在答案中使用代码,简单的Do 循环就可以正常工作。

标签: vba loops ms-word


【解决方案1】:

你可能会尝试基于:

Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "S U M M A R Y             "
    .Replacement.Text = ""
    .Forward = True
    .Format = False
    .Wrap = wdFindStop
    .Execute
  End With
  Do While .Find.Found
    Set Rng = .Duplicate
    Set Rng = Rng.GoTo(What:=wdGoToPage, Name:=.Information(wdActiveEndPageNumber))
    Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\Page")
    Rng.Paragraphs.Last.Range.InsertAfter Chr(12) & Chr(12)
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub

【讨论】:

  • 它添加了空白页,但第一行是下一页的文本。
  • 问题归结为你想如何定义这个空白页。另外,您还没有告诉我们相关页面末尾已经有什么。您可以将 Chr(12) 更改为 Chr(12) 和 Chr(12)。代码编辑
【解决方案2】:

一个简单的循环会为你工作:

Sub SelFind()


   ActiveDocument.Range.Select

    Do

    With Selection.Find
            .Text = "S U M M A R Y             "
            .Execute
    End With

        If Selection.Find.Found Then

            Selection.GoTo What:=wdGoToBookmark, Name:="\Page"
            Selection.MoveRight Unit:=wdCharacter, Count:=1
            Selection.MoveLeft Unit:=wdCharacter, Count:=1
            Selection.InsertBreak Type:=wdPageBreak

        Else: GoTo nxt

        End If
    Loop

nxt:

ActiveDocument.Range.Select

    Do

    With Selection.Find
            .Text = "R O Y A L T Y             "
            .Execute
    End With

        If Selection.Find.Found Then
        Dim Rnddg As Integer
            Rnddg = Selection.Information(wdActiveEndPageNumber)

            If Rnddg Mod 2 > 0 Then
                Selection.GoTo What:=wdGoToBookmark, Name:="\Section"
                Selection.MoveRight Unit:=wdCharacter, Count:=1
                Selection.MoveLeft Unit:=wdCharacter, Count:=1
                Selection.InsertBreak Type:=wdPageBreak
            End If

        Else: Exit Sub

        End If
    Loop



End Sub

【讨论】:

  • @Mikku,问题是我们能够获得“Royalty”的起始页,但我们必须获得结束页码。然后必须验证它是偶数还是奇数并添加一个空白页。
  • @RamSingh ...它应该找到所有出现的......因为我们正在开始一个全新的循环。如果现在不起作用,我建议您发布一个新问题。
  • @RamSingh .. 发布一个新问题 ... 我会在那里回答。
猜你喜欢
  • 2019-11-25
  • 1970-01-01
  • 2019-03-04
  • 1970-01-01
  • 1970-01-01
  • 2015-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多