1

これが私のコードですが、置き換えにはなりません!まず、TEMPを配列TargetListの要素に置き換えるサイクルを作成した後、AnyTextをTEMPに変更しますが、TEMPを置き換えることはできません。

Sub First()
'
' First Macro

    Dim i As Long
    i = 0
    Dim j As Long
    Dim myWord As String
    Dim msg As String
    myWord = "TEMP"
    TargetList = Array("AnyText", "NewWord1", "NewWord2", "NewWord3", "NewWord4") 
    For Each myStoryRange In ActiveDocument.StoryRanges
     With myStoryRange.Find
         .Text = "AnyText"
         .Replacement.Text = myWord
         .Wrap = wdFindContinue
         .Execute Replace:=wdReplaceAll
     End With
    Next myStoryRange

     For Each myStoryRange In ActiveDocument.StoryRanges
     With myStoryRange.Find
            Do While .Execute(FindText:=myWord, Forward:=True) _
                = True
                j = j + 1
            Loop
            msg = msg & "The string " & myWord & _
            " found " & j & " times."
        End With
        msg = msg & vbCrLf & vbCrLf
    MsgBox msg

     Next myStoryRange

       For Each myStoryRange In ActiveDocument.StoryRanges
     With myStoryRange.Find
         Do While j > -1
             .Text = "TEMP"
             .Replacement.Text = TargetList(i)
             msg = msg & "The string " & myWord & _
                " j = " & j & " i = " & i & TargetList(i) & " times."
              msg = msg & vbCrLf & vbCrLf
        MsgBox msg

             j = j - 1
             i = i + 1

             If i = 5 Then
                i = 0
             End If

         Loop
         .Wrap = wdFindContinue
         .Execute Replace:=wdReplaceAll
      End With
   Next myStoryRange
End Sub
4

1 に答える 1

1

このコードは、「AnyText」のインスタンスを「TEMP」に置き換え、次に「TEMP」のインスタンスを現在のmod5に置き換えます(count of finds)

Sub First()
    Dim i As Long
    i = 0
    Dim myWord As String
    myWord = "TEMP"
    TargetList = Array("AnyText", "NewWord1", "NewWord2", "NewWord3", "NewWord4")
    With ActiveDocument.Content.Find
         .Text = "AnyText"
         .Replacement.Text = myWord
         .Wrap = wdFindContinue
         .Execute Replace:=wdReplaceAll
    End With

    With ActiveDocument.Content.Find
        .Text = "TEMP"
        .Replacement.Text = TargetList(i)
        .Wrap = wdFindContinue
        Do While .Execute(Replace:=wdReplaceOne) = True
            i = i + 1
            If i = 5 Then i = 0
            .Replacement.Text = TargetList(i)
        Loop
    End With
End Sub

これにより、「AnyText」のすべてのインスタンスが変更されることに注意してください。たとえそれらがより大きな単語の一部であっても(たとえば、「myAnyTextWord」は「myTEMPWord」に変更されます)。単語全体を検索するだけの場合は、.MatchWholeWord = TrueWithブロックに追加します。

于 2012-11-28T18:34:46.017 に答える