Thread: [Solved] Search in Range Only Problem
View Single Post
 
Old 10-02-2014, 02:43 AM
gmayor's Avatar
gmayor gmayor is offline Windows 7 64bit Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

I have noticed this phenomenon and I cannot readily explain it, but you can work around it:

Code:
Dim oRng As Range
    Set oRng = ActiveDocument.Range
    With oRng.Find
        .Font.Bold = True
        .Text = Selection.Text
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
        Do While .Execute
            If oRng.Start = Selection.Start Then
                oRng.Text = "<start>" & oRng.Text & "<end>"
                Exit Do
            End If
        Loop
    End With
or even shorter
Code:
Dim oRng As Range
    Set oRng = ActiveDocument.Range
    With oRng.Find
        Do While .Execute(FindText:=Selection.Text)
            If oRng.Start = Selection.Start Then
                oRng.Text = "<start>" & oRng.Text & "<end>"
                Exit Do
            End If
        Loop
    End With
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote