2014-03-03 55 views
0

我有一个包含各种标题的文档。我希望能够做到以下几点:选择并删除Word标题到下一页打破宏VBA

  1. 从“标题1”样式中查找特定的标题。
  2. 删除标题,标题后面的内容并包括下一个分页符。

所以删除:标题+内容+分页

我知道如何查找文本使用Word宏,但我不知道该怎么只搜索标题。

Here is what needs to be deleted

你的帮助是极大的赞赏。

+1

'我知道如何使用Word宏查找文本,但我不知道如何仅搜索标题 - 与搜索文本相同,但将Find'对象的'.Style'属性设置为风格的名称。 – GSerg

+0

您的分页符手动放置?像“Page Break”这样的一系列点? – L42

回答

0

我发现你需要在选择之间启用扩展。

Selection.Find.ClearFormatting 
Selection.Find.Style = ActiveDocument.Styles("Heading 3") ' Search only "Heading 3" 
    With Selection.Find 
     .Text = "MIPwDMU" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchWildcards = False 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 
    End With 
    Selection.Find.Execute 
    Selection.Extend 
    Selection.Find.ClearFormatting 
    With Selection.Find 
     .Text = "^m" 
     .Forward = True 
     .Wrap = wdFindAsk 
     .Format = False 
    End With 
    Selection.Find.Execute 
    Selection.Delete 

这将删除标题和分页符之间的所有文本。