0

The code is meant to search through the current open Word document and select all instances of currently selected text. For example, I select "Casted" run the macro, and anywhere the text "Casted" appears, all of the instances are simultaneously selected [like ctr-clicking for multiple selections]

Sub SelectAllText()
    Dim searchText As String
    searchText = Selection.Text

    If searchText <> "" Then
        Dim foundRange As Range
        Set foundRange = ActiveDocument.Range

        With foundRange.Find
            .ClearFormatting
            .Text = searchText
            .Forward = True
            .Wrap = wdFindStop
            .MatchWholeWord = True
            .MatchCase = False
            .Execute

            Do While .found
                foundRange.Select 'what i needed to work
                foundRange.HighlightColorIndex = wdYellow 'This is just to highlight, not what i needed
                .Execute
            Loop
        End With
    End If
End Sub

So the code does selects the texts, but when the next instance is being selected, the previous one gets deselected. So at the end of the day only the last instance is the document remains selected, meanwhile i want everything to remain selected.

Oscar Sun
  • 1,427
  • 2
  • 8
  • 13
Gen
  • 15
  • 3
  • 2
    This is not possible. – Timothy Rylatt Aug 05 '23 at 10:31
  • 1
    I thought as much, coz nothing worked so far.. Thanks anyways – Gen Aug 05 '23 at 10:33
  • 1
    You could experiment with the .HitHighlight from in Selection.Find you can find it in the search dialog something like Reader Highlighting (left from search in) in German the button is named Lesehervorhebung. – Red Hare Aug 05 '23 at 11:25
  • @RedHare Thank you for the information that led me to get this answer. I was totally unaware that this method was available before. – Oscar Sun Aug 05 '23 at 14:47

1 Answers1

0

Just like Red Hare said, I tried the HitHighlight method of Find object and it worked.

Although this method can not really be like the Selection object or Select to be selected, it is still like the effect of the non-contiguous multi-selection. And after it runs, the range (the Parent of the Find object) does not changed and the Highlight effect is only the Highlight, not saved in the HighlightColorIndex property, thus it will not change the original document at all, ie. the Document's Saved property will not be changed to false. Also, it does not change the insertion position.

Sub SelectAllText()
    Dim searchText As String
    searchText = Selection.Text

    If searchText <> "" Then
        Dim foundRange As Range
        Set foundRange = ActiveDocument.Range

        With foundRange.Find
'            .ClearFormatting
'            .Text = searchText
'            .Forward = True
'            .Wrap = wdFindStop
'            .MatchWholeWord = True
'            .MatchCase = False
'            .Execute
            
'            Do While .Found
'                foundRange.Select 'what i needed to work
'                foundRange.HighlightColorIndex = wdYellow 'This is just to highlight, not what i needed
'                .Execute
'            Loop

            .ClearHitHighlight
            .HitHighlight FindText:=searchText, TextColor:=RGB(255, 255, 255), HighlightColor:=RGB(0, 0, 0), MatchCase:=True, MatchWholeWord:=True
            
        End With
    End If
End Sub

This should be the box and button that Red Hare mentioned. Thank him a lot!

enter image description here

Why remain selected

i want everything to remain selected.

And why do you want them to be selected? What I can guess are:

  1. If just for look like, then the HitHighlight method can fulfill.

  2. If you want to do something for them, just store each found range in a Collection for further use. Like my answer here:

https://stackoverflow.com/a/76192899/8249058

https://stackoverflow.com/a/76260672/8249058

...

Oscar Sun
  • 1,427
  • 2
  • 8
  • 13
  • 2
    Of course `HitHighlight` works, but it is nothing like "the effect of the non-contiguous multi-selection". It is simply a one-step method of applying a highlight to all the matches, without actually selecting anything. – Timothy Rylatt Aug 05 '23 at 17:11
  • I know @TimothyRylatt . Thanks so much. And why do we want them to be selected? What I can guess are: 1. If just for look like, then the **HitHighlight** method can fulfill. 2. If we want to do something for them, just store each found range in a **Collection** for further use. Aren't they? – Oscar Sun Aug 05 '23 at 18:31