“查找所有"方法 [英] 'Find all' method

查看:50
本文介绍了“查找所有"方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到文档中具有某种颜色的所有文本并在调试窗口中打印出来.

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

问题是它只给我下一个结果,而我想一次打印所有结果.据我所知,FindAll"方法不可用.也许我可以访问一个包含所有查找结果的数组.

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.

另外,稍微不相关,是否可以将所有结果复制到剪贴板而不是打印它们?

Also, slightly unrelated, would it be possible to copy all results to the clipboard instead of printing them?

推荐答案

您必须在循环中进行查找.请参阅此示例.我将查找结果存储在一个数组中

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

这篇关于“查找所有"方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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