删除ms word 2007中的某些行

时间:2013-10-03 17:20:27

标签: vba ms-word word-vba

enter image description here我想使用VBA宏删除word文档中的某些行。基本上要删除的(块)文本(并用“***”替换)遵循某种模式(下面)。

Bottom of Form
perma-link

Top of Form
save
Bottom of Form
[+] ....
[–] ....
Top of Form

“....”表示更改每个块的文本,但肯定该行以“[+]”或“[ - ]”开头。

请建议合适的宏

编辑:在截图中,我想将文本保留为黄色并删除其余部分。 (在实际文件中,文本不是黄色)

PS-FYI,我尝试使用示例looping a find and delete row macro(逐行删除)但是我得到一个运行时错误5941,调试选项突出显示宏中的“selection.row.delete”行。

这是什么意思?

1 个答案:

答案 0 :(得分:1)

假设示例列表是段落列表的开头,以下代码应该可以解决问题。您要做的就是将所有“段落开始”放入数组arrRemove,就像我为测试所做的那样。如果任何标记是特殊标记(请参阅this link for additional information),则需要像\[+]一样在其前面添加[-]。希望这是你正在寻找的。

Sub Macro2()

    Dim arrRemove As Variant
        arrRemove = Array("Bottom of Form", "perma -link", "Top of Form", _
                    "\[+\]", "\[\-\]", "Donec", "In")

    Dim i!
    For i = 0 To UBound(arrRemove)
        Activedocument.Range(0,0).select

        Selection.Find.ClearFormatting
        With Selection.Find
            .Text = arrRemove(i) & "*^13"
            .Replacement.Text = ""  'replace with nothing

            .Forward = True
            .Wrap = wdFindContinue
            .MatchCase = False
            .MatchWildcards = True
        End With
        Selection.Find.Execute Replace:=wdReplaceAll

    Next i

End Sub

以上宏将删除以下文档中的所有黄色段落。

enter image description here