Share via

Word Find first instance then replace

Anonymous
2024-11-14T00:44:05+00:00

Hello from Steve

In a single paragraph Samantha Wynne (Prebbleton) appears twice

objective change the first instance only.

Hopefully please the below can be modified

Meaning From Samantha Wynne (Prebbleton)

           To      Samantha Wynne 

Objective of below functions as it should.

Meaning From Samantha Wynne (Prebbleton)

           To      Samantha Wynne (Prebbleton)Samantha Wynne

Sub Insert_After_First_Instance()

Dim oRng As Word.range 

Dim arrWords 

Dim i As Long 

arrWords = Array("Samantha Wynne (Prebbleton)") 

arrInsertAfter = Array("Samantha Wynne") 

For i = 0 To UBound(arrWords)

    Set oRng = ActiveDocument.range 

    With oRng.Find 

        .ClearFormatting 

        .Replacement.ClearFormatting 

        .Text = arrWords(i) 

        .MatchWholeWord = True 

        .Replacement.Text = arrWords(i) & arrInsertAfter(i) 

        .Execute Replace:=wdReplaceAll 

    End With 

Next 

End Sub

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2024-11-14T06:17:47+00:00

Oh, in your original message, you mentioned "change the first instance only." So I thought you only needed to modify the first instance. I apologize for misunderstanding your requirement.

If you need to modify all instances, you can simply remove the part "arrWords(i) &" as mentioned in the image above.

Or you can copy the code below and overwrite your macro:

Sub Insert_After_First_Instance()  

    Dim oRng As Word.Range  

    Dim arrWords  

    Dim arrInsertAfter  

    Dim i As Long  

    arrWords = Array("Samantha Wynne (Prebbleton)")  

    arrInsertAfter = Array("Samantha Wynne")  

    For i = 0 To UBound(arrWords)  

        Set oRng = ActiveDocument.Range  

        With oRng.Find  

            .ClearFormatting  

            .Replacement.ClearFormatting  

            .Text = arrWords(i)  

            .MatchWholeWord = True  

            .Replacement.Text = arrInsertAfter(i) 

            .Execute Replace:=wdReplaceAll  

        End With  

    Next  

End Sub

Have a nice day!

Was this answer helpful?

0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Anonymous
    2024-11-14T06:41:38+00:00

    Hello Thomas

    I am very sorry I did not articulate as to what was required.

    All the Best

    Steve

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2024-11-14T06:03:08+00:00

    Hello Thomas

    Firstly thank you

    It finds the first instance but it does not find the others.

    Is it possible please to do the others,

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2024-11-14T04:55:03+00:00

    Hi Steve,

    This is our first meeting in November, and I am glad to see you again.

    I checked the code you shared, and the issue lies in concatenating the original text and the insert text during the replacement. You only need to remove the command that appends the original text in the replacement statement. 

    In addition, the macro will also replace all instances of "Samantha Wynne (Prebbleton)" with "Samantha Wynne (Prebbleton)Samantha Wynne." This does not meet your requirements. An additional layer of logic is needed to make the macro exit the loop after the first match.

    You can also copy the following code to replace your macro.

    Sub Replace_First_Instance()
    
        Dim oRng As word.Range
    
        Dim arrWords
    
        Dim arrInsertAfter
    
        Dim i As Long
    
        Dim found As Boolean
    
        arrWords = Array("Samantha Wynne (Prebbleton)")
    
        arrInsertAfter = Array("Samantha Wynne")
    
        For i = 0 To UBound(arrWords)
    
            Set oRng = ActiveDocument.Range
    
            With oRng.Find
    
                .ClearFormatting
    
                .Replacement.ClearFormatting
    
                .Text = arrWords(i)
    
                .MatchWholeWord = True
    
                .Forward = True
    
                .Wrap = wdFindContinue
    
                found = .Execute
    
                If found Then
    
                    oRng.Text = arrInsertAfter(i)
    
                End If
    
            End With
    
        Next
    
    End Sub
    

    Before:

    After:

    I hope my explanation makes sense, and I look forward to your reply!

    Best Regards,

    Thomas C - MSFT | Microsoft Community Support Specialist

    Was this answer helpful?

    0 comments No comments