vba “查找所有”方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10820184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'Find all' method
提问by Daan
I'd like to find all text in a document with a certain color and print it in the debug window.
我想在具有特定颜色的文档中查找所有文本并将其打印在调试窗口中。
Sub FindText()
Selection.Find.Font.Color = 3539877
Selection.Find.Execute
Debug.Print Selection
End Sub
The problem is that it only gives me only the next result while I want want to print all results at once. As far as I know, a 'FindAll' method is not available. Maybe I can access an array that contains all the find results.
问题是它只给我下一个结果,而我想一次打印所有结果。据我所知,“FindAll”方法不可用。也许我可以访问一个包含所有查找结果的数组。
Also, slightly unrelated, would it be possible to copy all results to the clipboard instead of printing them?
另外,稍微不相关,是否可以将所有结果复制到剪贴板而不是打印它们?
回答by Siddharth Rout
You have to do the find in a loop. See this example. I am storing the find results in an array
您必须在循环中进行查找。请参阅此示例。我将查找结果存储在一个数组中
Option Explicit
Sub FindText()
Dim MyAR() As String
Dim i As Long
i = 0
Selection.HomeKey Unit:=wdStory
Selection.Find.Font.Color = -671023105
Do While Selection.Find.Execute = True
ReDim Preserve MyAR(i)
MyAR(i) = Selection
i = i + 1
Loop
If i = 0 Then
MsgBox "No Matches Found"
Exit Sub
End If
For i = LBound(MyAR) To UBound(MyAR)
Debug.Print MyAR(i)
Next i
End Sub