繁体   English   中英

将查找限制为Word中的文本选择(而不是整个文档)

[英]Restricting find to a text selection in Word (not the entire document)

我试图将对Word文档中红色文本的搜索限制为单个表单元格。 无论我尝试什么,都将搜索整个文档。 我已经得到以下内容:

Sub FindRedText()
    Dim MyArray() As String
    Dim result As String
    Dim i As Long

'Dim RngFnd As Range

i = 0

Selection.SelectCell
'Set RngFnd = Selection.Range
Selection.Find.ClearFormatting
Selection.Find.Font.Color = wdColorRed

Do While Selection.Find.Execute = True
'If RngFnd.InRange(RngFnd) Then
        ReDim Preserve MyArray(i)
        MyArray(i) = Selection
        i = i + 1
'Else
'End If
    Loop

    If i = 0 Then
        MsgBox "No Red Text"
        Exit Sub
    End If

    For i = LBound(MyArray) To UBound(MyArray)
        result = Join(MyArray, ", ")
    Next i

End Sub

我留下了我的评论,以显示我尝试将当前选择设置为范围的尝试失败。 当我这样做时,单元格中的所有文本都将复制到数组中。

例如,说我有以下几点:

在表格单元1,1(已选择)中:“红色文本1”,“蓝色文本1”,“红色文本2”在表格单元1,2中:“红色文本3”

我的宏当前将“红色文本1”,“红色文本2”和“红色文本3”放入我的数组和最终结果字符串中。 我只想要“ red text1”和“ red text2”(即仅搜索所选单元格)。

尝试以下操作:首先选择区域,然后运行。

Sub FindSelectionColorText()
Selection.Find.ClearFormatting

With Selection.Find
    .Text = ""
    .Replacement.Text = ""
    .Font.Color = RGB(255, 0, 0)  'wdColorRed
    .Forward = True
    .Wrap = wdFindAsk
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
    Selection.Find.Execute
End Sub

'如果答案有效,则标记为答案和/或投票,否则请评论发生的情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM