Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding a paragraph containing a word in MS word [closed]

Tags:

regex

ms-word

I would like to find and possibly highlight all paragraphs in a .doc or docx document which contain one or more keywords.

I started with this regex (using MS Word's Find with wildcards) which simply finds all paragraphs:

([^13^l]){1}*(^13){1}

This works well in my document, i.e. it goes from paragraph to paragraph. Then I tried to replace the * with one possible search term *sust* like so:

([^13^l]){1}*sust*(^13){1}

This however highlights lots of paragraphs up to the end of a paragraph which includes *sust*. I find this very strange because the first attempt simply parsed the document paragraph by paragraph even in the parts that are returned by my second attempt.

I also tried ([^13^l]){1}(*sust*)(^13){1} as well as ([^13^l]){1} (*sust*) (^13){1} which all highlight lots of paragraphs rather than only one.

What am I doing wrong here?

like image 791
pandita Avatar asked Dec 04 '25 05:12

pandita


1 Answers

I was looking for the same solution and thought I'd share what worked great for me. Enter the VBA side of your Word doc (Alt + F11), Insert a new Module, Paste in this code, find the quote marks and enter whatever key word(s) you wish, hit the RUN arrow and watch it highlight all paragraphs that contain that key word. I'm very pleased with how it has helped me! Hope it also helps you and others!

Sub Highlight_Paragraph()

'THIS CODE HIGHLIGHTS TO THE END OF THE PARAGRAPH WHERE THE TARGET WORD
'IS FOUND WITHIN THE DOCUMENT

    Dim oRng As Range
    Set oRng = ActiveDocument.Range
    With oRng.Find
        Do While .Execute(FindText:="Contractor Shall")
            oRng.Paragraphs(1).Range.HighlightColorIndex = wdYellow
            oRng.Collapse 0
        Loop
    End With
lbl_Exit:
    Set oRng = Nothing
    Exit Sub
End Sub
like image 130
Tom Avatar answered Dec 07 '25 10:12

Tom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!