Results 1 to 8 of 8
  1. #1
    Star Lounger
    Join Date
    Aug 2015
    Posts
    98
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Highlight manually typed items in brackets

    Hi All
    I'm looking to add to a current macro I run on house style documents to highlight specific words to look out for when doing my checks - I'm looking for specific coding to be able to highlight manually typed items in brackets - as follows:

    (a), (b), (c), (d), (e)
    (i), (ii), (iii), (iv), (v)
    (1), (2), (3), (4), (5)

    The above are often contained within a numbered paragraph and then the above would need separating into separate numbered paragraphs but are often hard to locate when updating a document. Having them highlighted would enable them to stand out more and not be missed.

    Many thanks

  2. #2
    Star Lounger Graham Mayor's Avatar
    Join Date
    Mar 2016
    Location
    Cyprus
    Posts
    68
    Thanks
    0
    Thanked 24 Times in 24 Posts
    Maybe something like
    Code:
    Dim oRng As Range
        Set oRng = ActiveDocument.Range
        With oRng.Find
            Do While .Execute(FindText:="\([a-z0-9]{1,3}\)", MatchWildcards:=True)
                oRng.HighlightColorIndex = wdTurquoise
                oRng.Collapse 0
            Loop
        End With
    Graham Mayor - Word MVP
    http://www.gmayor.com

  3. #3
    Star Lounger
    Join Date
    Aug 2015
    Posts
    98
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Thank you Graham that has worked a treat

  4. #4
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,433
    Thanks
    371
    Thanked 1,456 Times in 1,325 Posts
    LL,

    You might want to change the term {1,3} to {1,5} as the {1,3} will not catch (viii) eight element if your documents go that deep!
    Note the 5 just gives one extra level.

    You also want to avoid words that are in parens so how about this modification. The first loop catches the Roman & Arabic Numerals and the second one the single letter (hoping you don't go more than 26 levels deep!) items. Note you could also add A-Z to this one if you have capital lettered items.
    Code:
    Sub Test()
    
    Dim oRng As Range
        Set oRng = ActiveDocument.Range
        With oRng.Find
            Do While .Execute(FindText:="(\([dcilmvx0-9]{1,5}\))", MatchWildcards:=True)
                oRng.HighlightColorIndex = wdTurquoise
                oRng.Collapse 0
            Loop
        End With
        Set oRng = ActiveDocument.Range
        With oRng.Find
            Do While .Execute(FindText:="(\([a-z]{1}\))", MatchWildcards:=True)
                oRng.HighlightColorIndex = wdTurquoise
                oRng.Collapse 0
            Loop
        End With
    End Sub
    LLRegEx.PNG

    For those RegEx experts out there I tried using the or (|) operator but couldn't get it to work plain or escaped! YMMV.

    HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  5. #5
    Star Lounger
    Join Date
    Aug 2015
    Posts
    98
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Thanks for the coding, much appreciated. I wondered whether I could go further with this in that it only highlights (a) or (i) or (1) when there is a space before and space after - at the moment it is highlighting everything within brackets such as section 1(a)(i)(1) for instance.

    I'm also trying to highlight any double straight quote marks that are not in bold.

    Thanks
    Shelley

  6. #6
    Star Lounger Graham Mayor's Avatar
    Join Date
    Mar 2016
    Location
    Cyprus
    Posts
    68
    Thanks
    0
    Thanked 24 Times in 24 Posts
    If you want to search only the bracketed numbers that have a space either side, then insert the spaces inside the search string, after the start quote and before the end quote e.g.
    Code:
    Dim oRng As Range
        Set oRng = ActiveDocument.Range
        With oRng.Find
            Do While .Execute(FindText:=" \([a-z0-9]{1,3}\) ", MatchWildcards:=True)
                oRng.HighlightColorIndex = wdTurquoise
                oRng.Collapse 0
            Loop
        End With
    If you also want to search for non-bold straight quotes then add
    Code:
    Set oRng = ActiveDocument.Range
        With oRng.Find
            .Font.Bold = False
            Do While .Execute(FindText:=Chr(34))
                oRng.HighlightColorIndex = wdYellow
                oRng.Collapse 0
            Loop
        End With
    See http://www.gmayor.com/replace_using_wildcards.htm
    Graham Mayor - Word MVP
    http://www.gmayor.com

  7. #7
    Star Lounger
    Join Date
    Aug 2015
    Posts
    98
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Hi Graham, thanks for the help much appreciated.

    What is the code to search for a bold left bracket and bold right bracket and include it in the highlight? I can't find what the code would be for brackets online. Reason being I'm coming across documents where the left, right or both are bold and in these docs they shouldn't be.

    Thanks, Shelley

  8. #8
    Star Lounger Graham Mayor's Avatar
    Join Date
    Mar 2016
    Location
    Cyprus
    Posts
    68
    Thanks
    0
    Thanked 24 Times in 24 Posts
    A bracket is simply a text string (unless it is in a wildcard search) - see http://www.gmayor.com/replace_using_wildcards.htm

    Code:
    Dim orng As Range
        Set orng = ActiveDocument.Range
        With orng.Find
            .Font.Bold = True
            Do While .Execute(FindText:="(")
                orng.HighlightColorIndex = wdTurquoise
                orng.Collapse 0
            Loop
        End With
        Set orng = ActiveDocument.Range
        With orng.Find
            .Font.Bold = True
            Do While .Execute(FindText:=")")
                orng.HighlightColorIndex = wdTurquoise
                orng.Collapse 0
            Loop
        End With
    Graham Mayor - Word MVP
    http://www.gmayor.com

Posting Permissions

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