PDA

View Full Version : [SOLVED:] Restricting find to a text selection in Word (not the entire document)



lebowski
10-11-2014, 04:45 AM
Hi, have been on a macro coding problem for days. Can someone please help? I am trying to restrict the search for red text in a Word document to a single table cell. Whatever I try, the whole document is searched. I have got as far as the following:



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


I have left my comments in to show my failed attempts to set the current selection as a range. When I do so, all the text in the cell is copied into the array.

As an example, say I have the following:
In table cell 1,1 (selected): "red text1", "blue text1", "red text2"
In table cell 1,2: "red text3"

My macro currently puts "red text1", "red text2" and "red text3" into my array and final result string. I just want "red text1" and "red text2" (i.e. a search of the selected cell only).

If anyone can help, I would be so grateful as I just cannot work out how this is done.

Thanks in advance.

gmaxey
10-11-2014, 05:13 AM
Sub FindRedTextII()
Dim arrStrings() As String
Dim strResult As String
Dim lngIndex As Long
Dim oRng As Range, oRngScope As Range
lngIndex = 0
Set oRng = Selection.Cells(1).Range
Set oRngScope = oRng.Duplicate
With oRng.Find
.ClearFormatting
.Font.Color = wdColorRed
Do While .Execute
If oRng.InRange(oRngScope) Then
ReDim Preserve arrStrings(lngIndex)
arrStrings(lngIndex) = oRng
oRng.Collapse wdCollapseEnd
lngIndex = lngIndex + 1
Else
Exit Do
End If
Loop
End With
If lngIndex = 0 Then
MsgBox "No Red Text"
Exit Sub
End If
strResult = Join(arrStrings, ", ")
MsgBox strResult

End Sub

lebowski
10-11-2014, 07:44 AM
Massive thanks Greg, I clearly have much to learn.

snb
10-11-2014, 08:48 AM
Sub M_snb()
With Selection.Find
.ClearFormatting
.Font.Color = 255
End With

If Selection.Information(12) Then
c01 = Selection.Information(16) & "," & Selection.Information(13)

Selection.HomeKey
Do
Selection.Find.Execute "", , , , , , , , True
If Selection.Information(16) & "," & Selection.Information(13) <> c01 Or Selection.Range.Font.Color <> 255 Then Exit Do

c00 = c00 & vbLf & Selection.Text
Selection.Collapse 0
Loop
End If

MsgBox "In cell (" & c01 & ")" & vbLf & c00
End Sub

gmaxey
10-11-2014, 09:26 AM
snb, why do you do it? The OP clearly states he has a solution and lots to learn. Then you come along with code, while perfectly functional, does little IMHO to advance learning.


Option Explicit
Sub M_snbExplained()
'Variables weren't declared. Since you claim you have a lot to learn _
it is not a bad practice to add Option Explicit as the first line in your module and declare all variables.
Dim strResult As String, strLocation As String
'Variables names were likely meaningless to anyone but snb. Use whatever convention you are comfortable with _
but meaninful names never hurt.
With Selection
'snb used all those numbers to stand in for constants. He may have all the constant numbers memororized, but _
I don't. If you have Tools>Options>Editors>AutoList members selected, you will see the contants after you _
type the "("
If .Information(wdWithInTable) Then
'
strLocation = .Information(wdStartOfRangeColumnNumber) & "," & .Information(wdStartOfRangeRowNumber)
.HomeKey
With .Find
.ClearFormatting
.Font.Color = 255
End With
Do While .Find.Execute
If .Information(wdStartOfRangeColumnNumber) & "," & .Information(wdStartOfRangeRowNumber) <> strLocation Then Exit Do
'Or Selection.Range.Font.Color <> 255 Then Exit Do (Don't know why snb used this. Maybe he will elaborate.)
strResult = strResult & vbLf & .Text
'Again snb substituted 0 for the constant.
.Collapse wdCollapseEnd
Loop
End If
End With
MsgBox "In cell (" & strLocation & ")" & vbLf & strResult
End Sub

fumei
10-11-2014, 08:49 PM
snb, why do you do it? The OP clearly states he has a solution and lots to learn. Then you come along with code, while perfectly functional, does little IMHO to advance learning. We would all like to know why. Considering snb never ever responds to comments, I suspect snb simply likes the sound of their own fingers on the keyboard. Relevancy or any real helpfulness has little to do with what is posted. More often than not posts seem deliberately submitted to HINDER learning.