An American Editor

December 29, 2014

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.

13 Comments »

  1. Hi Jack. Thanks for sharing your expertise so clearly and helpfully in this great series of articles. I too would use a macro to make these changes, but there is another way that doesn’t require one (which you most probably know about but which some of your readers might not). In the Find box, with Wildcards selected, type in the same search term – : [a-z] – but then click on ‘Find In’ and select ‘Main Document’. This will highlight all the relevant results throughout the active document. Closing out of ‘Find’ will remove the dialog box while keeping all the results highlighted. You can then use Shift + F3 on a PC, or Shift + Function + F3 on a Mac, to toggle the cases of all the selections simultaneously, until you get what you want. (In my version of Word, it only takes one toggle.)

    Like

    Comment by kevinobrien3 — January 1, 2015 @ 8:29 pm | Reply

    • Thanks, Kevin. What you’ve described is essentially the same thing as I suggested with the Find/Replace method–it changes case by changing the formatting. Why Microsoft decided to designate changing case as “formatting” is beyond me, but there it is. It would actually be fairly simple to create a macro that would toggle the actual case of a selection (in other words, change the actual character, not its case formatting). But hang on, hang on . . . Come to think of it, that’s one of the features I created in Editor’s ToolKit. Put your cursor on a word and press F10 to uppercase, or F11 to lowercase.

      http://www.editorium.com/ETKPlus2014.htm

      Best wishes,
      Jack

      Like

      Comment by Jack Lyon — January 2, 2015 @ 2:15 pm | Reply

      • Thanks, Jack. I thought Word’s in-built case-toggler shortcut (Shift + F3) did change the actual character, rather than its case formatting – so it would be good to know if I’ve got this wrong. Unlike applying ‘all caps’ formatting, when you use Word’s case toggler, selecting that letter or word again and pressing Control + Spacebar to reset the selection to the underlying style definition does not revert that letter to its original case by removing any direct formatting. Also, in a test file I made in which I changed the case in these two different ways and then saved as html, the case-toggler way shows the whole string as ‘normal’ text, whereas the ‘all-caps’ way shows the uppercased letter as a piece of applied formatting.

        Like

        Comment by kevinobrien3 — January 2, 2015 @ 4:59 pm | Reply

        • Hi, Kevin. I just tried what you suggested, and you’re absolutely right–toggling case *does* actually change the case of the selection. Evidently I’ve been confused about this for years, so I greatly appreciate the correction.

          Best wishes,
          Jack

          Like

          Comment by Jack Lyon — January 3, 2015 @ 3:00 pm | Reply

          • No problem, Jack. And your macro above is still the most efficient way of doing it, of course. Thanks again for your great Lyonizing Word series (especially the macros!), and I look forward to your future posts.

            Like

            Comment by kevinobrien3 — January 4, 2015 @ 5:36 am

          • But hang on, hang on . . . Here’s an interesting wrinkle:

            If we have a document with the following text–

            “This is a TEST.”

            –and we search for “TEST” and replace it with “^&” (the Find What Text code) formatted as “Not all caps,” we’ll still get “TEST” rather than “test”. And that’s true even if we specify “Match Case.”

            But if we have this–

            “This is a test.”

            –and we search for “test” and replace it with “^&” formatted as “All caps,” we *will* get “TEST.”

            In other words, it works one way but not the other.

            But here’s the real kicker: If we now *select* “TEST” (formatted as all caps during our Find/Replace) and press CTRL + SPACEBAR, the uppercase formatting *will* be removed, leaving us with “test”! Holy smoke!

            And that explains my confusion. I was actually right about “All caps” applied during Find/Replace as being formatting that can be wiped out. But I was wrong about the Change Case feature applying formatting.

            Microsoft Word never ceases to perplex.

            Like

            Comment by Jack Lyon — January 4, 2015 @ 7:40 pm

  2. Jack, Kevin, Rich, may I please have y’all’s permission to copy the above comment trail to a file for my future reference? I’d appreciate it.

    Jack, my dad told me about you and your extensive knowledge ages ago. (Well, not ages–we’re not quite that old.) Dad urged me to read your posts and eventually I started doing so. I’m glad to find you here, too, on Rich’s site.

    Like

    Comment by Camille DeSalme — January 6, 2015 @ 8:30 am | Reply

  3. Thank you Jack, this is exactly what I needed. I wasn’t aware of the ^& trick as well, will be handy in the future. For others who might run into something similar, I was getting a couple errors when I attempted to paste and run this. To fix it, I added parentheses around the colon in the find string, by changing the string from .Text = “: [a-z]” to .Text = “(:) [a-z]” Also, I’m not sure if it mattered, by I changed the smart quotes to straight quotes and removed the comments, as they were coming up in red and causing an error. Thanks again

    Like

    Comment by Rocky — March 21, 2016 @ 8:08 pm | Reply

    • Thanks, Rocky. Unfortunately, the WordPress blog engine changes straight quotes (both single and double) into curly ones, which, as you’ve seen, can really mess up a macro. The solution is to to change them back to straight quotes by hand. It’s a pain, but I’m glad you were able to work it out, and I’m glad the macro is useful to you.

      Like

      Comment by Jack Lyon — March 22, 2016 @ 12:58 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a free website or blog at WordPress.com.