VBA to look for more than 1 key word and highlights

Discussion in 'Word' started by ChrisOK, Feb 9, 2019.

  1. ChrisOK

    ChrisOK

    Joined:
    Sep 8, 2016
    Messages:
    12
    Likes Received:
    1
    This code is fabulous but it only seems to be able to do one key word at a time. I wish I'd asked how to do multiples when I was originally helped with this years ago - but didn't need it - now I do with a deadline over-head..
    Hoping someone or if still available @macropod can help me expand the capability to look for multiple key words/phrased rather than just one.
    (Example: "Delivered", "Architected", "Developed", "Resolved")
    Goal: extrapolate key statements from customer deliverables (status reports) over a full contract period's time -- and build a significant accomplishments list at end of contract using key several key words we're looking for)

    Thanks in advance!
    Chris

    Sub Highlight_ParagraphEx2AndCaseSensitiveorNot()

    'THIS ONE HIGHLIGHTS FULL PARAGRAPH

    Application.ScreenUpdating = False
    With ActiveDocument.Range
    With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "Delivered"
    .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
     
    ChrisOK, Feb 9, 2019
    #1
    1. Advertisements

  2. ChrisOK

    macropod Microsoft MVP

    Joined:
    Mar 2, 2012
    Messages:
    428
    Likes Received:
    37
    Try:
    Code:
    Sub Demo()
    Application.ScreenUpdating = False
    Dim ArrFnd(), i As Long, j As Long
    ArrFnd = Array("Delivered", "Architected", "Developed", "Resolved")
    For i = 0 To UBound(ArrFnd)
      With ActiveDocument.Range
        With .Find
          .ClearFormatting
          .Replacement.Text = ""
          .Replacement.ClearFormatting
          .Format = True
          .Forward = True
          .Wrap = wdFindStop
          .MatchWildcards = False
          .Text = ArrFnd(i)
          .Execute
        End With
        Do While .Find.Found
          j = i Mod 12
          If j < 6 Then
            j = j + 2
          Else
            j = j + 3
          End If
          With .Paragraphs.First.Range
            If .HighlightColorIndex > 0 Then .HighlightColorIndex = wdGray25
          End With
          .Start = .Paragraphs.First.Range.End
          .Find.Execute
        Loop
      End With
    Next
    Application.ScreenUpdating = True
    End Sub
    Note: I've also tweaked the code to use different highlights for each term. Up to 12 terms are catered for this way. If multiple terms appear in the same paragraph, that paragraph will get a grey highlight.
     
    macropod, Feb 9, 2019
    #2
    1. Advertisements

  3. ChrisOK

    ChrisOK

    Joined:
    Sep 8, 2016
    Messages:
    12
    Likes Received:
    1
    Thank you @macropod ! I created a New Module within my Word .docm, ran it but didn't get any result. What might I be doing wrong? I'm thinking many years ago you mentioned once that something had to be placed into the "ThisDocument" area in order to work for that particular item --- and alternatively, you mentioned turning on some setting in options I believe back when using Word 2016 but can't recall where... (not sure if the need to checkbox whatever setting this was -- still applies as I'm using Windows 365/Word)?

     
    ChrisOK, Feb 11, 2019
    #3
  4. ChrisOK

    ChrisOK

    Joined:
    Sep 8, 2016
    Messages:
    12
    Likes Received:
    1
    Update: @macropod -- I tested placing it in the "ThisDocument" area - but no luck... also kept searching and found the other Word 16.0 setting that you used to advise needed checked for certain scripts.. Found it under TOOLS > References and sure enough it was already checked... Attached is screenshot of what I have checked that may or may not be impacting - ? [​IMG]
    Image: https://app.box.com/s/lhaud5ok93boet5l5gmra32j51lu3t6b
     
    ChrisOK, Feb 11, 2019
    #4
  5. ChrisOK

    macropod Microsoft MVP

    Joined:
    Mar 2, 2012
    Messages:
    428
    Likes Received:
    37
    The macro runs fine for me regardless of what code module it's added to and without 'turning on some setting in options'. Are you sure the document you want to process was the active one when you ran the macro?
     
    macropod, Feb 11, 2019
    #5
  6. ChrisOK

    ChrisOK

    Joined:
    Sep 8, 2016
    Messages:
    12
    Likes Received:
    1
    Yes, here's a screenshot -- I only have a single Word doc open and the word "Delivered" is visible in the code and below in the document text - yet it's not highlighting... If I use the original code (with onsie words) using only "Delivered" it highlights all the references as intended... something odd between the orig code and the new code that's causing it not to highlight anything...?

    [​IMG]
    image direct link at Box: https://app.box.com/s/pdcjlax3pb8mlicad3ckjg8mbbt51ggk
     
    ChrisOK, Feb 12, 2019
    #6
  7. ChrisOK

    macropod Microsoft MVP

    Joined:
    Mar 2, 2012
    Messages:
    428
    Likes Received:
    37
    My bad. I seem to have dropped out some code. Replace:
    Code:
            If .HighlightColorIndex > 0 Then .HighlightColorIndex = wdGray25
    with:
    Code:
            If .HighlightColorIndex > 0 Then
              .HighlightColorIndex = wdGray25
            Else
              .HighlightColorIndex = j
            End If
     
    macropod, Feb 12, 2019
    #7
  8. ChrisOK

    ChrisOK

    Joined:
    Sep 8, 2016
    Messages:
    12
    Likes Received:
    1
    That's it! @macropod It works! THANKS GREATLY!!

    Once I get this deadline file out the door tonight, I'm going to try to figure out if there's an easier way to be able to type the desired search phrases into a pop-up (user dialog box) where I can quickly key them into like 5-10 diff fields, hit run and not have to worry about adjusting the back-end of the code tediously between the parens with every phrase requirement change...

    Heck, an even better idea would be using a table similar to what I did I manually with this effort... (image attached)
    I inserted an empty table on pg one of the Word doc -- 6 columns by 13 rows) and typed in all the ACTION verbs I was seeing as I skimmed through a 60 page doc. Then I bounced back and forth to the top of page 1 to grab another word from that table to type within the parens… (manual) Setting a business rule for the team to try to use a certain collection of verbs found in a similar table (preferably SMALLER than this) would also make this less painful for future..

    Have you ever seen code LOOK TO a table at the beginning of a doc and use it to INDEX through it and use words or short phrases in each cell to perform a function (such as highlighting?) Might be a pipe dream but if you think it can be done -- I'll go try to start searching for an example out there...

    (I know you know the rudiments/capabilities/bounds that it can and can not do --- so that's why I ask if it's a waste of my time to even try or not...)

    Image link: https://app.box.com/s/ywal2y7cyaailgfnlf0fm4s3y27kkb3p
     

    Attached Files:

    ChrisOK, Feb 12, 2019
    #8
  9. ChrisOK

    macropod Microsoft MVP

    Joined:
    Mar 2, 2012
    Messages:
    428
    Likes Received:
    37
    You could, of course, simply create an Index concordance file with all the terms you're interested in, then let Word index all those terms in the document. The Index concordance document is essentially a two-column table in which the texts to be indexed are in the first column and the form of the Index entries are in the second column. Done properly, the process is effective, straightforward, and efficient. See, for example:
    https://answers.microsoft.com/en-us...ord-2016/b22c25ec-0720-4403-97f5-418fc592862b
    See also: https://support.office.com/en-us/ar...an-index-cc502c71-a605-41fd-9a02-cda9d14bf073
     
    macropod, Feb 13, 2019
    #9
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.