用宏添加分页符(如果没有任何分页符)

时间:2016-03-07 06:28:07

标签: vba ms-word word-vba

我想在每个heading 1和evey \page书签之前添加分页符。 这是我的代码:

Sub PageBreack(isok)
    If isok <> True Then
         Exit Sub
    End If
    Selection.GoTo What = wdGoToLine, Which = wdGoToFirst

    Application.Browser.Target = wdBrowsePage
    For i = 1 To ActiveDocument.BuiltInDocumentProperties("Number of Pages")
        If i > 1 Then
            ActiveDocument.Bookmarks("\page").Range.Select
            Selection.InsertBreak Type:=wdSectionBreakContinuous  'wdSectionBreakNextPage
        End If
        Application.Browser.Next
    Next i
    For Each p In ActiveDocument.Paragraphs
      If Left(p.Style, 9) = "Heading 1" Then
        p.Range.Select
        Selection.Previous.InsertBreak Type:=wdSectionBreakContinuous
      End If
    Next
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "^12": .Replacement.Text = "^m": .Forward = True: .Wrap = wdFindContinue: .Format = False: .MatchCase = False: .MatchWholeWord = False: .MatchKashida = False: .MatchDiacritics = False: .MatchAlefHamza = False: .MatchControl = False: .MatchWildcards = False: .MatchSoundsLike = False: .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub  

我的问题是这段代码没有检查是否存在任何分页符并在那里添加新的分页符 如何更改我的代码宏检查是否有任何分页符添加分页符? (英语不是我的母语,我希望能清楚解释)

2 个答案:

答案 0 :(得分:1)

使用分页符宏可以显示空白页面 这是一个删除Word文档中所有空白页的宏

Sub Demo()
With ActiveDocument.Content.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "^12[^12^13 ]{1,}"
    .Replacement.Text = "^12"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
End With
End Sub

参考:Remove Blank Pages from Docx using word interop

Sub PageBreack(isok)
    If isok <> True Then
         Exit Sub
    End If
    Selection.GoTo What = wdGoToLine, Which = wdGoToFirst

    Application.Browser.Target = wdBrowsePage
    For i = 1 To ActiveDocument.BuiltInDocumentProperties("Number of Pages")
        If i > 1 Then
            ActiveDocument.Bookmarks("\page").Range.Select
            Selection.InsertBreak Type:=wdSectionBreakContinuous  'wdSectionBreakNextPage
        End If
        Application.Browser.Next
    Next i
    With ActiveDocument.Styles("Heading 1").ParagraphFormat
        .PageBreakBefore = True
    End With
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "^12": .Replacement.Text = "^m": .Forward = True: .Wrap = wdFindContinue: .Format = False: .MatchCase = False: .MatchWholeWord = False: .MatchKashida = False: .MatchDiacritics = False: .MatchAlefHamza = False: .MatchControl = False: .MatchWildcards = False: .MatchSoundsLike = False: .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    With ActiveDocument.Content.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "^12[^12^13 ]{1,}"
        .Replacement.Text = "^12"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchWildcards = True
        .Execute Replace:=wdReplaceAll
    End With
End Sub

答案 1 :(得分:0)

如果没有答案,您可以使用现有代码并再添加一个检查双分页符的步骤。

Selection.Find.Replacement.ClearFormatting

FORM bc_entry_log_fill