Consulting

Results 1 to 9 of 9

Thread: Highlight every character not followed by a space

  1. #1

    Question Highlight every character not followed by a space

    Hello all
    I have a strange request.
    I want to highlight any single character in my document that is not followed by a space.
    My document is like this:

    a b c s y op g d r j

    In the case above, it should apply a red text highlight to the character o because it is followed by another character and not a space.

    Is this possible, and if so, please could you point me in the right direction?
    Thank you kindly.

  2. #2
    The characters in the document are not Latin characters. They can be any unicode character.

  3. #3
    VBAX Contributor rollis13's Avatar
    Joined
    Jun 2013
    Location
    Cordenons
    Posts
    146
    Location
    You could use Word's "Replace" feature but with what I came up both characters will be highlighted, not only the first. This probably could only be done with a macro.
    Just to show what I mean. With a test file:
    In the menu 'Replace' in field 'Find what' type:
    ([x0000-x007F]{2})
    in field 'Replace with' type:
    \1
    then click 'More...' and check 'Use wildcards'
    and below in section 'Replace' in the dropdown 'Format' click 'Highlighted' (if Highlighted doesn't show up use instead 'Character' and 'Color character Yellow').
    That's all, now click button 'Replace All' to see the result.
    Since I don't exactly understand what you mean about 'any unicode character' in the 'Find' field I used code for Basic Latin. In this site you will find all the other codes: LINK so, if needed, you could, for example, indicate x0000-xFFFF as Unicode codes.
    Last edited by rollis13; 08-08-2022 at 02:52 PM.

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by rollis13 View Post
    You could use Word's "Replace" feature but with what I came up both characters will be highlighted, not only the first. This probably could only be done with a macro.
    Just to show what I mean. With a test file:
    In the menu 'Replace' in field 'Find what' type:
    ([x0000-x007F]{2})
    in field 'Replace with' type:
    \1
    then click 'More...' and check 'Use wildcards'
    and below in section 'Replace' in the dropdown 'Format' click 'Highlighted' (if Highlighted doesn't show up use instead 'Character' and 'Color character Yellow').
    That's all, now click button 'Replace All' to see the result.
    That approach, though, will highlight the second character also - even if that character is followed by a space. It also won't work properly on strings of 3 or more characters.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    For example:
    Sub Demo()
    Application.ScreenUpdating = True
    Dim Rng As Range
    For Each Rng In ActiveDocument.StoryRanges
      With Rng
        With .Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Forward = True
          .Wrap = wdFindStop
          .MatchWildcards = True
          .Text = "[! ^13^l^m^n^s^t]{2,}"
        End With
        Do While .Find.Execute
          .End = .End - 1
          .HighlightColorIndex = wdTurquoise
          .Collapse wdCollapseEnd
        Loop
      End With
    Next Rng
    Application.ScreenUpdating = True
    End Sub
    The above code finds and highlights any string of more than two characters, excluding white-space characters (spaces, tabs, paragraph breaks, etc.) not followed by a white-space character.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  6. #6
    This works beautifully Macropod! Thank you so much for sharing. I really appreciate it.
    And thank you rollis13 also for your solution!
    Last edited by Aussiebear; 08-09-2022 at 12:41 AM. Reason: removed unnecessary quoting

  7. #7
    Quote Originally Posted by macropod View Post
    For example:
    Sub Demo()
    Application.ScreenUpdating = True
    Dim Rng As Range
    For Each Rng In ActiveDocument.StoryRanges
      With Rng
        With .Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Forward = True
          .Wrap = wdFindStop
          .MatchWildcards = True
          .Text = "[! ^13^l^m^n^s^t]{2,}"
        End With
        Do While .Find.Execute
          .End = .End - 1
          .HighlightColorIndex = wdTurquoise
          .Collapse wdCollapseEnd
        Loop
      End With
    Next Rng
    Application.ScreenUpdating = True
    End Sub
    The above code finds and highlights any string of more than two characters, excluding white-space characters (spaces, tabs, paragraph breaks, etc.) not followed by a white-space character.

    The code excludes spaces, tabs, and paragraph breaks. Is there a way to also exclude the EN SPACE character?

    en space: U+2002 EN SPACE ( · UTF8: U+2002 (8194))

    These are the codes for the EN SPACE character, but I can't understand where to add it in the code above.

    Thank you once again for your help.

  8. #8
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    For that you could use:
    .Text = "[! ^13^l^m^n^s^t" & ChrW(8194) & "]{2,}"
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  9. #9
    Quote Originally Posted by macropod View Post
    For that you could use:
    .Text = "[! ^13^l^m^n^s^t" & ChrW(8194) & "]{2,}"
    Perfect, thank you so much macropod.

Posting Permissions

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