在页面包含使用Word的Word文档中的特定单词之后添加新页面

时间:2019-06-18 07:44:58

标签: vba ms-word word-vba

我想实现的目标是:

我需要在包含诸如“ S U M M A R Y 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

但是这样做是为了单页。您能帮我吗,我如何遍历所有页面。

2 个答案:

答案 0 :(得分:2)

一个简单的Do Loop将为您工作:

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

答案 1 :(得分:0)

您可以尝试基于以下条件进行尝试

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