Consulting

Results 1 to 5 of 5

Thread: Highlight All Words Containing Capital Letters?

  1. #1

    Highlight All Words Containing Capital Letters?

    Plagiarizing from this macro:
    http://www.vbaexpress.com/forum/show...-quot-and-quot

    ...I came up with this, which could probably be finessed, but does the basic job:

    Dim oRng As Range
    Set oRng = ActiveDocument.Range
    With oRng.Find
        Do While .Execute(FindText:="[A-Z]* ", MatchWildcards:=True)
            oRng.HighlightColorIndex = wdYellow
            oRng.Collapse 0
        Loop
    End With
    Pedantically, is it possible to avoid highlighting the next punctuation symbol and/or space?

    Thanks for any help!

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
    Dim oRng As Range
    Set oRng = ActiveDocument.Range
    With oRng.Find
      Do While .Execute(FindText:="[A-Z]*>", MatchWildcards:=True)
        oRng.HighlightColorIndex = wdYellow
        oRng.Collapse 0
      Loop
    End With
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    Amazing. Thank you!

  4. #4
    How to avoid the first word of the paragraph and the capital word succeeding sentence terminal (.!?), if the intention is only to find capital word caught between words?

    So there seems to be two issues?

    1. The capital word at the start of the paragraph
    2. The capital word that follows a punctuation and space

    A round-about way of achieving the result adapted from Greg Maxey code.

    Sub Highlight_capital()
         'Adapted from - A basic Word macro coded by Greg Maxey
        Dim oRng As Range
        Set oRng = ActiveDocument.Range
        With oRng.Find
            Do While .Execute(FindText:="( [A-Z]*>)", MatchWildcards:=True)
                oRng.HighlightColorIndex = wdYellow
                oRng.Collapse 0
            Loop
        End With
        
            Dim oRng1 As Range
        Set oRng1 = ActiveDocument.Range
        With oRng1.Find
            Do While .Execute(FindText:="([.\!?]) [A-Z]*>", MatchWildcards:=True)
                oRng1.HighlightColorIndex = wdNoHighlight
                oRng1.Collapse 0
            Loop
        End With
    End Sub
    Issues.

    If you plan to extract unique words, then noun clusters can pose a problem. (As in Output: Made it bold instead of highlight)

    Output

    To change the overall look of your document, choose new Theme elements on the Page Layout tab. To change the looks available in the Quick Style gallery, use the Change Current Quick Style Set command. Both the Themes gallery and the Quick Styles gallery provide reset commands so that you can always restore the look of your document to the original contained in your current template.

    Is there an elegant solution?
    Last edited by Programmer_n; 02-22-2017 at 07:03 PM.

  5. #5
    Belated thanks for an excellent addition. (Sadly inevitable) pedantic follow-up – is it possible to also...


    1) Un-highlight the space/punctuation preceding the first highlighted word?


    2) Highlight the first word of a sentence if the second word is highlighted?

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
  •