删除特定数量的文本,直到找到打开的段落

时间:2012-02-13 18:56:39

标签: vba text ms-word word-vba

我希望创建一个宏,允许我删除特定数量的文本,直到找到一个打开的段落。例如:

gateways (...)
ABC
DEF
GHJ
IKL

Other

我想通过搜索单词" gateways"来删除然后删除所有文本,直到找到"其他"之前的行。问题是文本的数量直到"其他"是从文档到文档的变量,我找不到允许我做错误的宏。

1 个答案:

答案 0 :(得分:0)

要将文本网关删除到空行,只留下其他

Public Sub DeleteFromTextToEmptyLine()
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "gateways"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    'find text
    If Selection.Find.Execute() Then
        'Selection.Collapse wdCollapseEnd 'remove comment to leave the word gateway intact
        'Turn on Extended select mode
        Selection.ExtendMode = True
        'find two paragraph marks together, i.e. empty line
        With Selection.Find
            .Text = "^p^p"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        'if found, delete the selection
        If Selection.Find.Execute() Then Selection.Delete
    End If
End Sub

如果您想离开单词网关,请从指定的行中删除注释