我想创建一个代码,该代码将在文档的标题中搜索特定的单词,并删除具有该特定单词的标题以及与之相关的所有内容。
例如:
如果我要查找的标题是“动物”,而子标题是“猫”和“狗”,则:
前宏
发布宏
谢谢
答案 0 :(得分:0)
您可以使用以下代码:
Sub DeleteHeadingBlocks()
Application.ScreenUpdating = False
Dim Rng As Range, i As Long, ArrRls
ArrRls = Array("Animal", "Vegetable", "Mineral")
For i = 0 To UBound(ArrRls)
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ArrRls(i)
.Style = wdStyleHeading1
.Replacement.Text = ""
.Forward = True
.Format = True
.Wrap = wdFindStop
.MatchWildcards = False
.Execute
End With
Do While .Find.Found
Set Rng = .Paragraphs(1).Range
Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel")
Rng.Delete
.Find.Execute
Loop
End With
Next
Set Rng = Nothing
Application.ScreenUpdating = True
End Sub
按照编码,宏假定您使用的是“标题1”样式。宏使用的数组提供三个术语(“动物”,“蔬菜”,“矿物质”);您可以删除/替换不需要的条款,或添加更多内容。