Lyonizing Word: Finding and Replacing Upper- and Lowercase

Finding and Replacing Upper- and Lowercase

by Jack Lyon

Rich Adin, the proprietor of this blog, recently sent me an interesting question. He wrote:

I need a wildcard find and replace, assuming it can be done by wildcards, that searches for the following

Abrams: alpha
booby: alarm

and replaces it with

Abrams: Alpha
booby: Alarm

That is, the first letter after the colon and space is changed from lowercase to uppercase. I know I can do this by macro, and I have one that will do it, but I would like to do it by wildcard so I can make it part of a script I run.

Unfortunately, there's no good way to do that. Using a wildcard search, we can find any lowercase letter (preceded by a colon and space) by using the following string in the Find What box:

: [a-z]

But in the Replace With box, we should use—what? We can't use the following string because it doesn't specify what the replacement letter should be:

: [A-Z]

In fact, if we try that, Word will simply replace what was found with the string itself, giving us this:

Abrams: [A-Z]lpha
booby: [A-Z]larm

There is, however, a rather sneaky (but ultimately unsatisfactory) workaround. We can replace the lowercase letter with itself formatted as uppercase. Here's how:

1. Press CTRL + H to bring up Word's Replace dialog.
2. If the More button is available, click it.
3. Put a check in the box labeled "Use Wildcards."
4. In the Find What box, enter this:

: [a-z]

5. In the Replace With box, enter this

^&

That's the magic code that tells Word to replace what was found with what was found. In other words, if Word finds ": a" it should replace it with ": a" (the same thing it was searching for). You'll see why in just a minute.

6. Make sure your cursor is in the Replace With box.
7. Click the Format button at the bottom left of the Replace dialog.
8. Click Font.
9. Put a check in the box labeled "All caps."
10. Click OK.
11. Click "Replace All."

That should do the trick; all of our lowercase letters following a colon and space are now formatted as "All caps." The reason I said earlier that this is "ultimately unsatisfactory" is that those letters are not actually uppercase; they merely look as if they're uppercase because of their formatting.

In some situations, that may be good enough. But if your document is destined to be published in a format other than Microsoft Word, it may not be good enough, as formatting may change and, like Cinderella at the stroke of midnight, our "uppercase" letters may revert to their true lowercase selves. (How often do we get to use a fairytale allusion in technical writing?)

The only real solution is to use a macro, like this one:

Sub ReplaceLowercaseWithCaps()
Selection.HomeKey Unit:=wdStory 'Position cursor at top of document
Selection.Find.ClearFormatting 'Clear any
Selection.Find.Replacement.ClearFormatting
With Selection.Find

.Text = ": [a-z]" 'Search for colon and space followed by lowercase letter
.Replacement.Text = "" 'Leave empty--the macro will replace the text later
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True 'Specify a wildcard search

End With
Selection.Find.Execute 'Execute the search
While Selection.Find.Found 'While the search is successful

Selection = UCase(Selection) 'Uppercase what was found
Selection.MoveRight 'Move out of the selection
Selection.Find.Execute 'Try, try again

Wend 'End the "While" loop
End Sub

I've added comments to explain what's going on, but the really pertinent line is this one:

Selection = UCase(Selection) 'Uppercase what was found

When Word finds a colon and space followed by a lowercase letter, it selects the colon, space, and letter (naturally, because it found them), so those are the "Selection." The macro then converts those characters to uppercase using the "UCase" function; it sets the Selection as the uppercased version of the Selection, if you see what I mean.

After that, the macro moves to the right so the text is no longer selected. Then it again executes the Find in an effort to locate the next instance of colon, space, and lowercase letter, if one exists.

And yes, for the sake of simplicity, the colon and space are uppercased here as well as the letter. What's an uppercased colon? A colon. What's an uppercased space? A space. If we wanted to, we could modify the macro to handle each of those separately, but why bother when the result is the same? Virtue in simplicity.

Note that we could do the inverse of this, if we needed to, finding any uppercase letter and lowercasing it. To do so, we'd use ": [A-Z]" for the search string, and we'd modify Selection with the LCase function rather than UCase.

I wish that Microsoft had included a better way to handle this. Even though Microsoft didn't, we now have a way to accomplish what we need to do.

Jack Lyon (editor@editorium.com) owns and operates the Editorium, which provides macros and information to help editors and publishers do mundane tasks quickly and efficiently. He is the author of Microsoft Word for Publishing Professionals and of Macro Cookbook for Microsoft Word. Both books will help you learn more about macros and how to use them.

________

How to Add Macro to Word & to the QAT

Here’s how to put this macro (or any other) into Microsoft Word so it will be available when you need it:

  1. Copy the text of the macro, starting with the first “Sub” and ending with the last “Sub.”
  2. Click the “View” tab on Microsoft Word’s ribbon.
  3. Click the “Macros” button.
  4. Type a name for the macro in the “Macro name” box — probably the name used after the first “Sub.” For this macro, that’s “ReplaceLowercaseWithCaps.”
  5. Click the “Create” button.
  6. Delete the “Sub [macro name]” and “End Sub” lines that Word created in the macro window. The macro window should now be completely empty (unless you already have other macros in there).
  7. Paste the macro text at the current insertion point.
  8. Click “File,” then “Close and Return to Microsoft Word.”

To actually use the macro:

  1. Place your cursor at the beginning of your document.
  2. Click the “View” tab on Microsoft Word’s ribbon.
  3. Click the “Macros” button.
  4. Click the name of your macro to select it.
  5. Click the “Run” button. (If you wanted to delete the macro, you could press the “Delete” button instead.)

Here’s how to put the macro on Word’s QAT (Quick Access Toolbar):

  1. Locate the QAT (it’s probably on the top left of your screen either above or below Word’s Ribbon interface).
  2. Right-click the QAT.
  3. Click “Customize Quick Access Toolbar.”
  4. Under “Choose commands from:” click the dropdown list and select “Macros.”
  5. Find and select your macro in the list on the left.
  6. Click the “Add” button to add it to the QAT.
  7. Click the “OK” button to finish.
This entry was posted in Computers and Software, Contributor Article, Editorial Matters, Lyonizing Word and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

13 Comments