删除不同的短语

时间:2016-03-22 07:56:28

标签: vba ms-word word-vba

我尝试用空字符串“”:

替换以下短语(更进一步到150)
[1]
[2]
...
[150]

<1>
<2>
...
<150>

<1/>
<2/>
...
<150/>

如果不是很多代码,以下解决方案将起作用。如何简化这个用值“null”替换每个提到的短语?

 Sub Encoder()
'
' Encoder Makro
'
'
    Selection.Find.Execute Replace:=wdReplaceAll
    With Selection.Find
        .Text = "[1]"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
        With Selection.Find
        .Text = "[2]"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
...... >>>> and so on with all the other phrases
End Sub

2 个答案:

答案 0 :(得分:0)

您可以使用for循环遍历要更改的所有整数。 在下面的代码片段中,'i'是循环存在后的起始编号higEnd。 如果从Debug行中删除“'”,则可以看到连接的字符串。

Sub renameYourNumbers(higherEnd As Integer)

        Dim i As Integer

        i = 1
        For i = 1 To higherEnd

            Selection.Find.Execute Replace:=wdReplaceAll
            With Selection.Find
            .Text = "[" & CStr(i) & "]"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindAsk
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            End With

            'Debug.Print ("[" & CStr(i) & "]")
        Next

End Sub

您可以通过

调用此功能
Call renameYourNumbers(150)

答案 1 :(得分:0)

为了您的需要,最好使用外卡来处理您的模式。 试试这个

Sub Demo()
    With ActiveDocument.Range.Find
        .Text = "[\<\(\{\[\\\/]{1,2}[0-9]{1,5}[\>\}\]\)\\\/]{1,}"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .MatchWildcards = True
        .Execute Replace:=wdReplaceAll
    End With
End Sub

表示1-150,试试这个

Sub renameYourNumbers()

        Dim i As Integer

        i = 1
        For i = 1 To 150

            Selection.Find.Execute Replace:=wdReplaceAll
            With Selection.Find
            .Text = "[\<\(\{\[\\\/]{1,2}" & CStr(i) & "[\>\}\]\)\\\/]{1,2}"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .MatchWildcards = True
            .Execute Replace:=wdReplaceAll

            End With

            'Debug.Print ("[" & CStr(i) & "]")
        Next

End Sub