宏创建多个分页符而不是一个

时间:2015-10-08 07:27:33

标签: vba word-vba

我创建了一个宏,它找到样式H1的实例,后跟样式H2,并在它们之间插入分页符。并重申这一点,直到它到达文档的末尾。

但是,宏添加了多个分页符(可能是50个分页符)而不是一个。

我做错了什么以及如何纠正这个错误?

代码:

Sub Force_page_break()
'
' Force_page_break Macro
'
'
    Dim Eloop As Integer
    Eloop = 1
    Selection.HomeKey Unit:=wdStory
    Do While Eloop = 1
    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Heading 1")
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchKashida = False
        .MatchDiacritics = False
        .MatchAlefHamza = False
        .MatchControl = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    If Selection.Find.Execute = False Then
    Eloop = 0
    Exit Do
    End If
    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Heading 2")
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchKashida = False
        .MatchDiacritics = False
        .MatchAlefHamza = False
        .MatchControl = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.HomeKey Unit:=wdLine
    Selection.InsertBreak Type:=wdPageBreak
    Loop
    ActiveDocument.Save
 End Sub

1 个答案:

答案 0 :(得分:0)

也许你可以添加一行

Eloop = Eloop + 1
行后

Selection.InsertBreak Type:=wdPageBreak

退出循环?