循环基于文档中的重复文本

时间:2017-01-16 15:03:23

标签: vba ms-word word-vba

我有一个由许多章节组成的Word文档。文档没有样式,一切都是文本,但每章都以“章#”(没有引号)开头。我记录并调整了一个宏,它可以找到下一章#,块,副本,剪切上一章,打开一个新文档,粘贴,保存和关闭。我必须单击每章的宏。我如何制作它,所以我只点击一次,宏重复下一章,并在没有剩下时停止?这就是我所拥有的:

Sub Breakout2()  
'  
' Breakout2 Macro  
'  
'  
   Selection.Find.ClearFormatting  
    With Selection.Find  
        .Text = "^pChapter "  
        .Replacement.Text = ""  
        .Forward = True  
        .Wrap = wdFindContinue  
        .Format = False  
        .MatchCase = True  
        .MatchWholeWord = False  
        .MatchWildcards = False  
        .MatchSoundsLike = False  
        .MatchAllWordForms = False  
    End With  
    Selection.Find.Execute  
    Selection.Find.Execute  
    Selection.HomeKey Unit:=wdStory, Extend:=wdExtend  
    Selection.Cut  
    Documents.Add DocumentType:=wdNewBlankDocument  
    Selection.PasteAndFormat (wdUseDestinationStylesRecovery)  
    ChangeFileOpenDirectory "U:\Breakout\"  
    ActiveDocument.SaveAs2  
    ActiveDocument.Close  
End Sub  

1 个答案:

答案 0 :(得分:0)

通过更改我的代码来解决:

Sub Breakout2()  
'  
' Breakout2 Macro  
'  
'  
Dim i
For i = 1 To 1000

{then same code as in OP}

Next i
End Sub