Share via

Word find text in window2

SteveD 535 Reputation points
2026-04-29T21:49:56.0466667+00:00

Hello from Steve

What is required please for the below script to function.

Find text in window2

Sub Testing2()

Dim i As Integer

Dim blnFound As Boolean

Selection.Find.ClearFormatting

With Selection.Find

    .Text = "[0-9]{1,} [A-Z]{1,}"

    .Replacement.Text = ""

    .Forward = True

    .Wrap = wdFindContinue

    .Format = False

    .MatchCase = False

    .MatchWholeWord = False

    .MatchAllWordForms = False

    .MatchSoundsLike = False

    .MatchWildcards = True

End With

Selection.Find.Execute
```Selection.Fields.Update

strText = Selection.Text

Selection.Collapse Direction:=wdCollapseStart

Windows(2).Activate

With Selection.Find

.Forward = True

.Wrap = wdFindContinue

.MatchCase = True

.MatchWholeWord = True

.Text = strText

.MatchWildcards = False

For i = 1 To 2

blnFound = .Execute

If Not blnFound Then

    MsgBox "Occurrence " & i & " was not found in Window 2!", vbExclamation

    Exit Sub

End If

Next i


Selection.HomeKey Unit:=wdLine

Selection.Extend

Selection.EndKey

Selection.Copy

MsgBox "The 2nd occurrence has been copied successfully!", vbInformation

End Sub

Developer technologies | Visual Basic for Applications

Answer accepted by question author

AI answer

2026-04-29T21:50:11.3566667+00:00

The script needs two main things to work reliably:

  1. Ensure the wildcard Find in the first window actually succeeds and returns text.
  2. Use the Find object correctly when searching in Window(2).

Below is a corrected and simplified version based on the standard Word Selection.Find pattern from the documentation, including wildcard use and a second Find using the captured text:

Sub Testing2()

    Dim i As Integer
    Dim blnFound As Boolean
    Dim strText As String

    ' First window: find pattern with wildcards
    With Selection.Find
        .ClearFormatting
        .Text = "[0-9]{1,} [A-Z]{1,}"
        .Replacement.ClearFormatting
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True

        blnFound = .Execute
    End With

    If Not blnFound Then
        MsgBox "No match found in Window 1.", vbExclamation
        Exit Sub
    End If

    ' Optional: update fields in the found selection
    Selection.Fields.Update

    ' Capture the found text
    strText = Selection.Text

    ' Collapse selection so next operations start at beginning of match
    Selection.Collapse Direction:=wdCollapseStart

    ' Activate second window and search for the same text
    Windows(2).Activate

    With Selection.Find
        .ClearFormatting
        .Text = strText
        .Replacement.ClearFormatting
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = True
        .MatchWholeWord = True
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = False

        ' Find the 1st and 2nd occurrence
        For i = 1 To 2
            blnFound = .Execute
            If Not blnFound Then
                MsgBox "Occurrence " & i & " was not found in Window 2!", vbExclamation
                Exit Sub
            End If
        Next i
    End With

    ' Copy the entire line of the 2nd occurrence
    Selection.HomeKey Unit:=wdLine
    Selection.Extend
    Selection.EndKey Unit:=wdLine
    Selection.Copy

    MsgBox "The 2nd occurrence has been copied successfully!", vbInformation

End Sub

Key points aligned with Word’s Find model in the documentation:

  • Selection.Find.ClearFormatting and .Replacement.ClearFormatting are called before setting criteria, as shown in the examples.
  • Wildcards are enabled only for the first search (.MatchWildcards = True) and disabled for the second (.MatchWildcards = False).
  • Execute is used on the Find object and its Boolean return value (blnFound) is checked, matching the documented pattern for Find.Execute.
  • Wrap = wdFindContinue is used so the search continues through the document if the end is reached.

If Windows(2) does not exist or is not a different view of the same document, the macro will fail at Windows(2).Activate; ensure a second window is open for the document before running the macro.


References:

AI-generated content may be incorrect. Read our transparency notes for more information.

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.