Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    Jan 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Find Key Word and Highlight Entire Paragraph that Follows

    Searching all day to locate something that will find a key word or phrase then highlight (in yellow) the entire paragraph that follows that key word or phrase.
    Ideally, if it could also pick up any listed items that follow the paragraph, that would be awesome, but I won't push my luck.. Doing just the paragraph is still an exceptional improvement to my measly line.

    The code I am using only highlights to the end of the line. =-(
    I need it to identify a long list of requirements buried within Word documents and having the ability to highlight the listed details would be incredible but not mandatory.
    (example):
    "The Contractor shall turn in monthly status reports within ten business days after the end of each month. The report should include:
    (a) Accomplishments
    (b) Meetings and Outcomes
    (c) Completed Travel and Purpose of Travel

    Here's the code I use to highlight to the end of the line:
    Code:
    Sub Find_Highlight_Word_to_End_of_Line()
    'BUT NEED IT TO HIGHLIGHT THROUGH END OF PARAGRAPH
    'AND HIGHLIGHT LISTED ITEMS IF APPLICABLE
    'LIKE THE LISTS IN THE EXAMPLE DOCUMENT
    Dim sFindText As String
    'Start from the top of the document
     Selection.HomeKey wdStory
     
    sFindText = "Contractor Shall"
    Selection.Find.Execute sFindText
    Do Until Selection.Find.Found = False
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
            Selection.Range.HighlightColorIndex = wdYellow
            Selection.MoveRight
            Selection.Find.Execute
    Loop
    End Sub
    The attachment shows more of what I hope to achieve or something close to this extent of highlighting...
    Thanks in advance!
    Attached Files Attached Files

  2. #2
    Gold Lounger Maudibe's Avatar
    Join Date
    Aug 2010
    Location
    Pa, USA
    Posts
    2,637
    Thanks
    115
    Thanked 650 Times in 592 Posts
    ChrisOK,

    Not sure if this completely does what you want to do but try adding the code line highlighted in blue which will extend the highlight to the end of the paragraph (carriage return).

    HTH,
    Maud

    Code:
    Sub Find_Highlight_Word_to_End_of_Line()
    'BUT NEED IT TO HIGHLIGHT THROUGH END OF PARAGRAPH
    'AND HIGHLIGHT LISTED ITEMS IF APPLICABLE
    'LIKE THE LISTS IN THE EXAMPLE DOCUMENT
    Dim sFindText As String
    'Start from the top of the document
     Selection.HomeKey wdStory
     
    sFindText = "authenticated"
    Selection.Find.Execute sFindText
    Selection.Extend (Chr(13))
    Do Until Selection.Find.Found = False
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
            Selection.Range.HighlightColorIndex = wdYellow
            Selection.MoveRight
            Selection.Find.Execute
            
    Loop
    End Sub

  3. #3
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    A wildcard search is a quick and dirty way to do what you asked but not clever enough to do what you highlighted in your sample document.

    This code finds a few capitalisation variants and goes to the end of each paragraph. It is NOT smart enough to:
    - go backwards to the start of the sentence
    - go forwards if the paragraph is introducing list items
    Code:
    Sub Macro2()
      Options.DefaultHighlightColorIndex = wdYellow
      With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Replacement.Highlight = True
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False    'this is disabled in wildcard searches
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
        .Execute Replace:=wdReplaceAll, FindText:="CONTRACTOR SHALL*^13"
        .Execute Replace:=wdReplaceAll, FindText:="contractor shall*^13"
        .Execute Replace:=wdReplaceAll, FindText:="Contractor shall*^13"
      End With
    End Sub
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Also cross-posted and answered at:
    http://www.office-forums.com/threads...llows.2349709/
    http://www.vbaexpress.com/forum/show...ound-That-Word
    as well as being cross-posted at: http://www.msofficeforums.com/word-v...paragraph.html

    For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

    Kindly notify ALL boards on which you've cross-posted that you have multiple solutions, providing links between them to all the other threads in which you've cross-posted.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. #5
    New Lounger
    Join Date
    Jan 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Maud and Andrew for demonstrating some new alternatives! & Paul cross-post help!

    When I tested Maud's - I found that it indeed highlighted to the end of the line.
    When I tested Andrew's - I found that it also highlighted to the end of the line but missed one occurrence of "Contractor Shall" (probably because it was underlined and in italics and we would need to define to look for those specifics in order to pick them up? Is that assumption correct?
    I've learned so much from seeing diff ways of achieving! (and now ways to be more specific with definitions when needed)

    Here's 2 ways I've discovered that achieve the paragraph highlighting and notes that may also help others:

    Example 1:

    Code:
    Sub Highlight_ParagraphEx1()
    
    '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
    
    '===============================================================
    'SOME ADDITIONAL NOTES ON HOW THIS WORKS FROM MARK007 (gmaxey)
    ' on my post: http://www.vbaexpress.com/forum/showthread.php?57092-Find-Word-then-Highlight-Entire-Paragraph-and-or-Sentence-Around-That-Word
    '---------------------------------------------------------------
    'Note: Word doesn't have a clear idea what a sentence is all about.
    'Where the code above would highlight. Blah,blah. [John Smith the contractor shall blah. Blah blah]
    'it would process Blah, blah. Mr. [Smith the contractor shall blah. Blah, blah.
    
    'because Word confuses the period after Mr. as a sentence stop.
    
    'No (1) is not one paragraph to the right. It is the first paragraph in the ranges paragraph collection. So when the range is defined as a word or snippet in a paragraph the range.Paragraphs(1) is that ranges parent paragraph.
    
    'Collapse 0 is simple short for Collapse wdCollapseEnd where wdCollapseEnd is constant with the value = 0
    'You collapse the range so you don't get stuck in a loop finding the same thing over and over again.
    'lbl_Exit:
    'Exit Sub
    
    'Is just my style that my good friend Graham liked and adapted.
    'It doesn 't serve much purpose other that to tell me that I have at least thought more than absently about what the code does and serves as an exit loop:
    
    'On Error GoTo lbl_Err
    'Err.Raise 6
    'lbl_Exit:
    'Exit Sub
    'lbl_Err:
    'Msgbox Err.Number & " " Err.Description
    'Resume lbl_Exit
    'End Sub
    Example 2:

    Code:
    Sub HighlightParagraphEx2AndCaseSensitiveorNot()
    
    'THIS ONE ALSO HIGHLIGHTS FULL PARAGRAPH BUT HAD TO
    'CHANGE THE MATCHWILDCARDS = TRUE TO (FALSE) IN ORDER TO GET IT TO WORK AS NEEDED
    Application.ScreenUpdating = False
    With ActiveDocument.Range
      With .Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "Contractor Shall"
      .Replacement.Text = ""
      .Forward = True
      .Wrap = wdFindStop
      .Format = True
      .MatchWildcards = False
      .Execute
      End With
      Do While .Find.Found
      .Duplicate.Paragraphs.First.Range.HighlightColorIndex = wdYellow
      .Start = .Duplicate.Paragraphs.First.Range.End
      .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub
    
    'Using wildcards, makes the Find/Replace case-sensitive.
    'If that's not what you want, change '.MatchWildcards = True' to '.MatchWildcards = False'.
    'macropod (Paul Edstein) on office forums: http://www.office-forums.com/threads/vba-to-find-word-or-phrase-then-highlight-entire-paragraph-that-follows.2349709/#post-7430664

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •