View Single Post
 
Old 01-08-2018, 10:14 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,144
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

Restricting to the current section is only a little more complicated.
Code:
Sub Macro2()
Const strWord As String = "word to find"
Dim oRng As Range
Dim oFind As Range
Dim oSection As Section
Set oSection = Selection.Sections(1)    'the section the cursor is in
Set oFind = oSection.Range    'set a range to the section
    With oFind.Find
        'look for each occurrence of the word in turn
        Do While .Execute(FindText:=strWord, MatchCase:=True, MatchWholeWord:=True)
            If oFind.InRange(oSection.Range) Then    'the word is in the section
                oFind.End = oFind.Paragraphs(1).Range.End    'move the end of the range to the end of the paragraph
                'containing the word.
                Set oRng = oFind.Next.Paragraphs(1).Range    'set a range to the following paragraph
                oRng.End = oRng.End - 1    'omit the paragraph break character (and/or the section break)
                oRng = Replace(oRng, "(", "")    'delete the first unwanted character
                oRng = Replace(oRng, ")", "")    'delete the second unwanted character
                oFind.Collapse 0    ' collapse the found text range to its end
            Else
                GoTo lbl_Exit    'the found word is not in the section
            End If
        Loop
    End With
lbl_Exit:
    Set oSection = Nothing
    Set oFind = Nothing
    Set oRng = Nothing
    Exit Sub
End Sub
__________________
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