Word Macro Pattern Matching

I needed to highlight all the abbreviations in my book. So, I wrote a macro in Microsoft Word to do that. Pattern Matching is usually done using regular expressions. Regular expressions represent patterns in a string. [A-Z] looks for the uppercase characters A through Z in a string. If that pattern exists, then the match is true.

My beef with regular expressions is that almost every programming language has their own unique flavor of patterns for the regular expressions. These patterns are all similar, but they’re not the exactly the same. At least I’ve never seen any two that are exactly the same. Since I program in many languages, I try to avoid regular expressions, unless they’d be really useful. Otherwise I’d need to look up the meaning. Also, since not all programmers are familiar with regular expressions, you need to document your expressions in case you’re not the one who has to change them. Anyone who’s not familiar with them will likely say, what’s all that gobbledygook?

Usually there are functions to do what what you want to do rather than using regular expressions. Why reinvent the wheel.

All my abbreviations were all uppercase characters. All were 2, 3, or 4 characters long. So I just needed a macro to highlight all words which fit those criteria.
In most languages you’d use functions like IsAlpha, IsUpper, and Length. Word macros are written in VBA (Visual Basic for Applications). Word has a IsNumeric function, a UCase function, and a length function. I tried combining those first. But the macro highlighted $4K and ../ as abbreviations. I had too many of those and I wanted to make the macro do what I wanted it to do. I tried to use IsAlpha, but there’s no such function – at least that’s what the internet says.

Then I remembered regular expressions. There are regular expressions in Visual Basic (VB), but not in VBA. You can create a regular expressions object and then use regular expressions, but that felt a little clunky. And it was cumbersome for just doing the little bit I wanted to do.

Then I found the LIKE operator. VB and VBA both use the LIKE operator. LIKE matches patterns. However, the pattern representation is slightly different. In VB I would have used the pattern "^[!A-Z]+$" which means check all characters and see if any of them are not A-Z. With the LIKE operator, the pattern is much simpler "*[!A-Z]*". In this case, the * on both ends tell it to check all characters. The ! means not.

Here is the short macro I wrote to highlight all my abbreviations

Sub WrF_Abbreviations()
'Only highlights uppercase abbreviations
If Selection.Type = wdSelectionIP Then 'If nothing selected then select everything
Selection.WholeStory
End If
For Each wordi In Selection.Words 'Loop through all words in selection
SingleWord = Trim(wordi) 'Trim spaces from current word
If Len(SingleWord) > 1 And Len(SingleWord) < 5 Then 'If length = 2,3,4
If SingleWord Like "*[!A-Z]*" Then 'Skip if contains anything other than A-Z
Else
wordi.HighlightColorIndex = wdYellow 'Found abbreviation so highlight it
End If
End If
Next wordi
End Sub

Of course, you’ll need my other macro to turn off highlighting:

Sub WrF_Clear_Highlighting()
If Selection.Type = wdSelectionIP Then
Selection.WholeStory
End If
Selection.Range.HighlightColorIndex = wdNoHighlight
End Sub

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x