Word 2007宏自动编号文档中的项目

时间:2012-06-27 20:28:45

标签: vba word-2007

我正在尝试使用宏来查找和替换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

帮助?

1 个答案:

答案 0 :(得分: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