在 Array 中存储多个选择,然后选择 Array 中的所有选择 [英] Store multiple selections in Array and later do select all the selections in the Array

查看:39
本文介绍了在 Array 中存储多个选择,然后选择 Array 中的所有选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找单词并将所选内容保存在数组中,然后再次查找并将下一个选择保存在数组中.最后尝试选择数组中的所有选项.

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.

我尝试过的解决方案:

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

上面的问题是 selecttest(2) 只被选中.我想要同时选择 selecttest(1) 和 selecttest(2) 的最终结果

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

我也试过

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

解决方案:不可能

发现一些关于不连续范围选择的文章是 VBA 不可能的,而 FindAll 是可能的

在 VBA 中查找所有内容:https://forums.windowssecrets.com/showthread.php/124485-Find-All-in-VBA

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

原因 1:其中谈到了 findall

Reason 1: which talks about findall

遗憾的是,微软在Word 的 VBA 对象模型.也就是说,无法执行 Find All来自宏.

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.

您可以在 VBA 中遍历所有出现的搜索文本,但是这与查找全部"不同.

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

原因 2:findall 与不可能的不连续选择密切相关

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

可能 Find All 不在 VBA 中的原因是 VBA 也从来没有有办法处理不连续的选择(那种你可以使用 Ctrl 和鼠标创建),这就是 Find All 会产生的结果.这里的知识库文章解释了可以做的几件事.每一个自 2002 年(包括 2010 年)以来的版本在这方面没有做任何更改.

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.

原因 3:在处理和内存方面的计算成本很高.如果由 VBA 完成

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

我怀疑这个遗漏是有意的,并且是经过仔细考虑的.在打开文档窗口的视觉上下文,查找全部是一个完美的明智的概念.然而,在 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.

这并不是说它不能在 VBA 中完成,只是我可以明白为什么没有完成.对于它的价值,同样如此Excel 中的 Find 对象,我对它有更深入的了解,以及最近,经验.

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.

要在 VBA 中支持 Find All 将需要 Execute 方法返回Range 对象的集合,可以在计算上昂贵,在处理和内存方面

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

推荐答案

Dim oRng As Word.Range
Set oRng = Selection.Range
oRng.Find.ClearFormatting
With oRng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "shhada"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchKashida = False
        .MatchDiacritics = False
        .MatchAlefHamza = False
        .MatchControl = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
         While .Execute
               oRng.Editors.Add wdEditorEveryone
         Wend
         ActiveDocument.SelectAllEditableRanges wdEditorEveryone
         ActiveDocument.DeleteAllEditableRanges wdEditorEveryone
    End With 
End Sub

这篇关于在 Array 中存储多个选择,然后选择 Array 中的所有选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆