PDA

View Full Version : Highlight Phrases Containing Capital Letters?



VB-AN-IZ
10-24-2017, 12:35 AM
Related to:
http://www.vbaexpress.com/forum/showthread.php?57443-Highlight-All-Words-Containing-Capital-Letters

This code will highlight any words which contain a capital letter:


Sub highlight_capitals()
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
End Sub

Note that it highlights not just the words containing capital letters, but also the spaces between any consecutive words that contain capital letters. So, "United States" would be highlighted; not "United" and "States" separately.

I'm hoping to expand it by being able to specify common phrases like "of the", or a hyphen, so that they are also highlighted when they occur between words which contain capital letters.

For example:

Currently, for "State of the Nation", only "State" and "Nation" are highlighted.


Currently, for "World Anti-Doping Code", only "World Anti" and "Code" are highlighted.


Hope that makes sense, and thanks for any help!

macropod
10-24-2017, 05:47 AM
The following is a more efficient version of the basic code that will also highlight 'World Anti-Doping Code' and other strings with spaces between the words.

Sub highlight_capitals()
Options.DefaultHighlightColorIndex = wdYellow
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Format = True
.MatchWildcards = True
.Replacement.Text = "^&"
.Replacement.Highlight = True
.Text = "<[A-Z][A-Za-z0-9]@>"
.Execute Replace:=wdReplaceAll
.Text = "<[A-Z][A-Za-z0-9]@>[ -]<[A-Z][A-Za-z0-9]@>"
.Execute Replace:=wdReplaceAll
End With
End Sub
Unless you know how many intervening words there are in the phrases, it isn't possible to construct F/R expressions that deal with them. And, if you do, you open a possible can of worms with unintended matches.

VB-AN-IZ
10-29-2017, 09:56 PM
Thanks for this. I tried adding a few lines to the macro like:


.Text = "<[A-Z][A-Za-z0-9]@> of the <[A-Z][A-Za-z0-9]@>"
.Execute Replace:=wdReplaceAll


...but, as you said, it would then only highlight up to the next or previous word either side of "of the", if those words contained a capital letter.

Next thought – is it possible to work backwards, as in step three below?


1) Highlight all instances of "of the", as well as the space between them and either side.
2) Use the original macro to highlight all words containing capitals, and the spaces between them.
3) UN-highlight instances of "of the" that are NOT surrounded by a word containing capitals?


Thanks again.

macropod
10-30-2017, 03:51 AM
...but, as you said, it would then only highlight up to the next or previous word either side of "of the", if those words contained a capital letter.
Aside from the fact the word following ' of the' has to start with a capital, not just contain one, I'd have thought from the previous discussion that's precisely what you wanted to do.

Next thought – is it possible to work backwards, as in step three below?

1) Highlight all instances of "of the", as well as the space between them and either side.
2) Use the original macro to highlight all words containing capitals, and the spaces between them.
3) UN-highlight instances of "of the" that are NOT surrounded by a word containing capitals?
Aside from requiring a lot more work to get the same result, I can't see what the purpose of that would be.