Store multiple selections in Array and later do select all the selections in the Array

て烟熏妆下的殇ゞ 提交于 2019-12-24 19:53:04

问题


I am trying to find a word and save the selection in an array and then find again and then save the next selection in the array. And in the end try to select all the selections in the array.

I am trying this but its with half knowledge. I am not able to get it. Can some one help.

Sub Macro6()
'
' Macro6 Macro
'
'
Selection.HomeKey Unit:=wdStory
Dim selecttest(2) As Selection
For I = 1 To 2
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "PQXY"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
    End With
    Selection.Find.Execute
    Set selecttest(I) = Selection
    Selection.MoveRight Unit:=wdCharacter, Count:=1
Next I
For I = 1 To 2
  selecttest(I).Select
Next I
End Sub

I want to keep the selection in the loop and show them in the end.

Solution i tried:

Sub Macro61()
'
' Macro6 Macro
'
'
Selection.HomeKey Unit:=wdStory
Dim selecttest(2) As Range
For i = 1 To 2
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "PQXY"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
    End With
    Selection.Find.Execute
    Set selecttest(i) = Selection.Range
    Selection.MoveRight Unit:=wdCharacter, Count:=1
Next i

For i = 1 To 2
selecttest(i).Select
Next i
End Sub

Problem above is selecttest(2) is only selected. I want the end result with both selecttest(1) and selecttest(2) selected

I also tried

Dim totalselect as Range
For i = 1 to 2
set totalselect = totalselect + selectest(i)
Next i

totalselect.select

It shows error that "+" (plus) operation does not exist

Solution: Not Possible

Found some articles regarding discontinous range selections is not possible by VBA whereas its possible by FindAll

Find All in VBA: https://forums.windowssecrets.com/showthread.php/124485-Find-All-in-VBA

Reason 1: which talks about findall

Unfortunately, Microsoft omitted to add support for "Find All" in the VBA object model for Word. In other words, Find All cannot be executed from a macro.

You can loop through all occurrences of the search text in VBA, but that's not the same as Find All.

Reason 2: findall is inderectly related to discontiguous selections which is not possible

Probably the reason a Find All isn't in VBA is that VBA also has never had any way to deal with discontiguous selections (the kind you can make with Ctrl and the mouse), which is what Find All would produce. The KB article here explains the few things that can be done. Every version since 2002 (including 2010) has made no changes in this area.

Reason 3: computationally expensive, in terms of both processing and memory. if done by VBA

I suspect this omission was intentional and carefully considered. In the visual context of an open document window, Find All is a perfectly sensible concept. However, in the procedural world of VBA, it is a tad more difficult to work with sets of things, and, often, computationally less efficient.

This isn't to say that it can't be done in VBA, only that I can understand why it wasn't done. For what it's worth, the same is true of the Find object in Excel, with which I have much more intimate, and recent, experience.

To support Find All in VBA would require the Execute method to return a collection of Range objects, which could be computationally expensive, in terms of both processing and memory


回答1:


Try with two simple changes replacing Selection into Range object:

Sub Macro6()
    ...
    ...
    Dim selecttest(2) As Range 'not Selection
    ...
    ...
    Set selecttest(i) = Selection.Range 'not just a Selection
    ...
End sub



回答2:


If all you want to do is show the matches, you only need:

ActiveDocument.Range.Find.HitHighlight FindText:="PQXY"


来源:https://stackoverflow.com/questions/54881150/store-multiple-selections-in-array-and-later-do-select-all-the-selections-in-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!