0

我正在尝试使用宏来查找和替换 Word 2007 文档中的某些文本。每次找到文本时,我想在替换文本中自动增加一个数字。

前文:

The quick brown (??) fox jumps over (??) the (??) lazy dog

之后的所需文字:

The quick brown (1) fox jumps over (2) the (3) lazy dog

出于某种原因,我的代码只替换了第一个实例:

Sub SetRequirements()
    Dim myNumber As Integer
    myNumber = 1

    With ActiveDocument.Content.Find
        .ClearFormatting
        .Text = "(??)"
        Do While .Execute( _
            Replace:=wdReplaceOne, _
            ReplaceWith:="(" & myNumber & ")", _
            Forward:=True) = True

            myNumber = myNumber + 1

        Loop
    End With
End Sub

帮助?

4

1 回答 1

2
Sub SetRequirements()

Const S_FIND As String = "(??)"
Dim myNumber As Integer

    myNumber = 1

    Do While InStr(ActiveDocument.Content, S_FIND) > 0
        With ActiveDocument.Content.Find
            .ClearFormatting
            .Text = S_FIND
            .Execute Replace:=wdReplaceOne, ReplaceWith:="(" & myNumber & ")", _
                     Forward:=True
        End With
        myNumber = myNumber + 1
    Loop

End Sub
于 2012-06-27T21:01:18.747 回答