Word VBA:如何通过替换对象仅替换字符串的下一个实例

Word VBA:如何通过替换对象仅替换字符串的下一个实例,vba,ms-word,Vba,Ms Word,这是一个愚蠢的问题,但我想不通 直接从Microsoft网站: 此示例在活动文档中查找单词“Start”的每个实例,并将其替换为“End”。查找操作忽略格式设置,但与要查找的文本(“Start”)的大小写匹配。 Set myRange = ActiveDocument.Range(Start:=0, End:=0) With myRange.Find .ClearFormatting .Text = "Start" With .Replacement .ClearFormatting

这是一个愚蠢的问题,但我想不通

直接从Microsoft网站:

此示例在活动文档中查找单词“Start”的每个实例,并将其替换为“End”。查找操作忽略格式设置,但与要查找的文本(“Start”)的大小写匹配。

Set myRange = ActiveDocument.Range(Start:=0, End:=0)
 With myRange.Find
.ClearFormatting
.Text = "Start"
With .Replacement
    .ClearFormatting
    .Text = "End"
End With
.Execute Replace:=wdReplaceAll, _
    Format:=True, MatchCase:=True, _
    MatchWholeWord:=True
End With

我需要知道如何使它只找到
Start
的下一个实例,并将其替换为
End
。这将使整个文档中的所有其他
结尾保持不变。

此讨论提供了一些有用的建议:。简言之,这是一种从开始到找到搜索参数的第一个实例的循环搜索字符串的情况,并仅替换该实例。

此讨论提供了一些有用的建议:。简言之,这是一种从开始到找到搜索参数的第一个实例的循环搜索字符串的情况,并仅替换该实例。

此讨论提供了一些有用的建议:。简言之,这是一种从开始到找到搜索参数的第一个实例的循环搜索字符串的情况,并仅替换该实例。

此讨论提供了一些有用的建议:。简言之,这是一种从开始到找到搜索参数的第一个实例都在搜索字符串中循环并替换的情况。

您应该使用
wdReplaceOne
代替
wdReplaceAll

您应该使用
wdReplaceOne
代替
wdReplaceAll

您应该使用
wdReplaceOne
代替
wdReplaceAll

您应该使用
wdReplaceOne
代替
wdReplaceAll

您应该能够适应以下情况:

Sub Tester()

    Const FIND_WHAT as String = "Start"
    Const REPLACE_WITH as String = "End"

    Const REPLACE_WHICH As Long = 4 'which instance to replace?

    Dim rng As Range, i As Long

    i = 0
    Set rng = ActiveDocument.Content
    With rng.Find

        .ClearFormatting
        .Text = FIND_WHAT 

        Do While .Execute(Format:=True, MatchCase:=True, _
                           MatchWholeWord:=True)

            i = i + 1
            If i = REPLACE_WHICH Then
                'Note - "rng" is now redefined as the found range
                '  This happens every time Execute returns True
                rng.Text = REPLACE_WITH 
                Exit Do
            End If

        Loop

    End With

End Sub

您应该能够适应以下情况:

Sub Tester()

    Const FIND_WHAT as String = "Start"
    Const REPLACE_WITH as String = "End"

    Const REPLACE_WHICH As Long = 4 'which instance to replace?

    Dim rng As Range, i As Long

    i = 0
    Set rng = ActiveDocument.Content
    With rng.Find

        .ClearFormatting
        .Text = FIND_WHAT 

        Do While .Execute(Format:=True, MatchCase:=True, _
                           MatchWholeWord:=True)

            i = i + 1
            If i = REPLACE_WHICH Then
                'Note - "rng" is now redefined as the found range
                '  This happens every time Execute returns True
                rng.Text = REPLACE_WITH 
                Exit Do
            End If

        Loop

    End With

End Sub

您应该能够适应以下情况:

Sub Tester()

    Const FIND_WHAT as String = "Start"
    Const REPLACE_WITH as String = "End"

    Const REPLACE_WHICH As Long = 4 'which instance to replace?

    Dim rng As Range, i As Long

    i = 0
    Set rng = ActiveDocument.Content
    With rng.Find

        .ClearFormatting
        .Text = FIND_WHAT 

        Do While .Execute(Format:=True, MatchCase:=True, _
                           MatchWholeWord:=True)

            i = i + 1
            If i = REPLACE_WHICH Then
                'Note - "rng" is now redefined as the found range
                '  This happens every time Execute returns True
                rng.Text = REPLACE_WITH 
                Exit Do
            End If

        Loop

    End With

End Sub

您应该能够适应以下情况:

Sub Tester()

    Const FIND_WHAT as String = "Start"
    Const REPLACE_WITH as String = "End"

    Const REPLACE_WHICH As Long = 4 'which instance to replace?

    Dim rng As Range, i As Long

    i = 0
    Set rng = ActiveDocument.Content
    With rng.Find

        .ClearFormatting
        .Text = FIND_WHAT 

        Do While .Execute(Format:=True, MatchCase:=True, _
                           MatchWholeWord:=True)

            i = i + 1
            If i = REPLACE_WHICH Then
                'Note - "rng" is now redefined as the found range
                '  This happens every time Execute returns True
                rng.Text = REPLACE_WITH 
                Exit Do
            End If

        Loop

    End With

End Sub
“下一个实例”相对于什么?你是指文档中的第二个实例,还是其他所有实例?“下一个实例”相对于什么?你是指文档中的第二个实例,还是其他所有实例?“下一个实例”相对于什么?你是指文档中的第二个实例,还是其他所有实例?“下一个实例”相对于什么?您是指文档中的第二个实例,还是指其他所有实例?