Share via

Word 8 REVISION find in second window 8 36951 REVISION

SteveD 430 Reputation points
2026-04-29T22:08:43.05+00:00

Hello from Steve

Objective

Window 1 has 8 REVISION

Window 2 has 8 36951 REVISION

Is it possible please from Window 1 8 REVISION to find in Window 2 8 36951 REVISION

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

Developer technologies | Visual Basic for Applications
0 comments No comments

Answer accepted by question author

  1. Kai-H 16,355 Reputation points Microsoft External Staff Moderator
    2026-04-30T07:43:29.2833333+00:00

    Hi, SteveD

    Yes, it is possible, but not by searching Window 2 with the exact text from Window 1.

    Right now your code finds 8 REVISION in Window 1, stores that exact text in strText, then looks for the exact same text in Window 2. That will not match 8 36951 REVISION because Word sees it as different text.

    What you need in Window 2 is a wildcard search, for example:

    .Text = "8 [0-9]{1,} REVISION"
    .MatchWildcards = True
    

    Or, if you want to build it from strText, replace the space with a wildcard numeric block.

    Also, it is recommended that you use a Range instead of Selection where possible. Selection depends on the active window and can jump around, while Range.Find is usually more stable for this kind of cross-window search.

    So the main fix is: don’t search Window 2 for the literal strText, search for a wildcard pattern that allows digits between 8 and REVISION.

    This matches the documented behavior of Word VBA Find, where Selection.Find changes the active selection, while Range.Find redefines the range without changing selection, and Word wildcard search supports patterns like {n,} for repeated digits.

    Thank you for your patience in reading, I hope this information has been helpful to you. 


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment."    

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. 

    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.