Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA单词我想用这个+;循环中的不同单词_Vba_Ms Word - Fatal编程技术网

VBA单词我想用这个+;循环中的不同单词

VBA单词我想用这个+;循环中的不同单词,vba,ms-word,Vba,Ms Word,这是我的代码,但它不能替换! 首先,我将AnyText更改为TEMP,然后用数组TargetList的元素循环替换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", "N

这是我的代码,但它不能替换! 首先,我将AnyText更改为TEMP,然后用数组TargetList的元素循环替换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

此代码将“AnyText”的任何实例替换为“TEMP”,然后将“TEMP”的任何实例替换为当前的
(查找计数)
mod 5:

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=True
添加到带有
块的每个

此代码将用“TEMP”替换“AnyText”的任何实例,然后用当前
替换“TEMP”的任何实例(查找计数)
mod 5:

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=True
添加到每个

使用
故事范围是否有特定原因。查找
而不是
单词。查找
文档内容。查找
?你的MSGBox是否显示while循环正常运行?不,我没有理由。MsgBoxes显示while循环是正确的。您使用
StoryRange.Find
而不是
Word.Find
Doc.Content.Find
是否有特殊原因?你的MSGBox是否显示while循环正常运行?不,我没有理由。MsgBoxes显示while循环是正确的。