2012-06-27 89 views
0

我正嘗試使用宏來查找並替換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 

幫助?

回答

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 
+0

+1對於精彩。我正在玩這個試圖學習更多的WORD VBA ......並且我的錯誤和你的答案,我做到了!另外,測試這個,它的工作原理! –

相關問題