View Full Version : [SOLVED:] Highlight every character not followed by a space
Jimduggan
08-08-2022, 09:40 AM
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.
Jimduggan
08-08-2022, 09:42 AM
The characters in the document are not Latin characters. They can be any unicode character.
rollis13
08-08-2022, 02:30 PM
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 (https://unicode-table.com/en/blocks/) so, if needed, you could, for example, indicate x0000-xFFFF as Unicode codes.
macropod
08-08-2022, 03:22 PM
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.
macropod
08-08-2022, 04:05 PM
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.
Jimduggan
08-08-2022, 07:40 PM
This works beautifully Macropod! Thank you so much for sharing. I really appreciate it.
And thank you rollis13 also for your solution!
Jimduggan
08-10-2022, 09:29 AM
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.
macropod
08-10-2022, 10:26 PM
For that you could use:
.Text = "[! ^13^l^m^n^s^t" & ChrW(8194) & "]{2,}"
Jimduggan
08-11-2022, 07:52 AM
For that you could use:
.Text = "[! ^13^l^m^n^s^t" & ChrW(8194) & "]{2,}"
Perfect, thank you so much macropod.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.