View Single Post
 
Old 02-13-2019, 06:44 AM
scienceguy scienceguy is offline Windows 10 Office 2016
Advanced Beginner
 
Join Date: Feb 2019
Posts: 46
scienceguy is on a distinguished road
Default How to return the full string (entire word) when searching with wildcards in word vba

I need to search a document for "words" that start with "32P". However, the numbering after the "32P" will be different (32P1, 32P2, 32P21, etc.). I can write a macro that finds all the "32P" occurrences by using a wildcard ("32P*"). However, I can't figure out how to get the full string (or full "word"). This is what I have so far:

Code:
Sub FindWords2()
        sResponse = "32P*"
        
        If sResponse <> "" Then
            Application.ScreenUpdating = False
            With Selection
                .HomeKey Unit:=wdStory
                With .Find
                    .ClearFormatting
                    .Text = sResponse
                    .MatchWildcards = True
                    ' Loop until Word can no longer
                    ' find the search string and
                    Do While .Execute
                        MsgBox Selection.Range.Text 'This only returns "32P" and not the whole string
                        Selection.MoveRight
                    Loop
                End With
            End With
            Application.ScreenUpdating = True
        End If
End Sub
Any coaching would be greatly appreciated! Thanks in advance!
Reply With Quote