Today’s column by Jack Lyon marks the first essay in a new monthly series, “Lyonizing Word.” In this series, Jack will discuss using Microsoft Word, especially macros, to best advantage during the editing process. Please welcome Jack as a new columnist for An American Editor.
______________
Lyonizing Word: The Next Character Macro
by Jack Lyon
Macros and mastering Microsoft Word are keys to success in the business of editing. One can be a great editor and not master either, but it is more difficult, if not near-impossible, to have a successful editing business if you aren’t master of the tools you use.
My plan is to help you master Word and Word macros. My hope is that you will learn from each of my columns and will take the lessons learned and build on them yourself — for example, by building more complex and more useful macros that fulfill a need in your editing business. Here’s my first installment, which I hope you’ll find useful.
The NextCharacter Macro
I often use character codes while finding and replacing in Microsoft Word. What’s a character code? Here are some common ones:
^09 is a tab
^13 is a carriage return
^32 is a space
Especially in a wildcard search, you may have to use such codes because Word’s wildcard search engine can’t handle certain characters and will give you an error message if you try to use them.
But what if you don’t know the code for the character you need? You can probably look it up online or in a computer book, but wouldn’t it be nice to have a macro that would tell you immediately? Just put your cursor in front of the character and run the macro to get the code number. Here’s a macro that will do just that:
Sub NextCharacter()
Dim NextChar As String
NextChar = Str(AscW(Selection))
MsgBox “The code for the next character is” & NextChar
End Sub
That’s pretty simple, but there’s still a lot going on. Let’s look at each line in the macro.
Sub NextCharacter()
Here we tell Word the name of the macro, which is a subroutine (Sub) named NextCharacter. You can use pretty much any name you like, as long as it doesn’t start with a number or include any spaces. And the parentheses at the end of the name? I’ll reserve that discussion for a future article.
Dim NextChar As String
I just made up the name NextChar but not the commands around it, all of which are part of VBA (Visual Basic for Applications), Microsoft’s programming language for Word and other programs in the Office suite. If you want to learn more about “Dim,” “As,” “String,” and other VBA commands, here’s a good place to start: Getting Started with VBA in Word 2010.
In this line, we’re “dimensioning” (Dim) a variable to hold a value (NextChar) that we’ll use later in the macro. A variable is just a placeholder that can hold a value, like X in your high-school algebra class. Dimensioning just tells Word what *kind* of value we’re using — in this case, a string of characters rather than, say an integer, which can only hold numbers.
NextChar = Str(AscW(Selection))
This assigns a value to the NextChar variable. What value? The one produced by this:
Str(AscW(Selection))
“Selection” is the character to the right of your cursor (in the Western world).
“AscW” tells Word to find the ASCII or Unicode value of that character.
“Str” turns that value into a string — that is, characters rather than a value. Why? So we can see those characters in the message box produced by the next line:
MsgBox “The code for the next character is” & NextChar
This displays a message box (MsgBox) that gives us the code for the next character (as stored in NextChar), preceded by the text “The code for the next character is” just to pretty things up.
End Sub
This line simply ends the macro, or subroutine.
Now, here’s how to put this macro (or any other) into Microsoft Word so it will be available when you need it:
- Copy the text of the macro, starting with the first “Sub” and ending with the last “Sub.”
- Click the “View” tab on Microsoft Word’s ribbon.
- Click the “Macros” button.
- Type a name for the macro in the “Macro name” box — probably the name used after the first “Sub.” For this macro, that’s “NextCharacter.”
- Click the “Create” button.
- 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).
- Paste the macro text at the current insertion point.
- Click “File,” then “Close and Return to Microsoft Word.”
To actually use the macro:
- Place your cursor directly in front of the character you want to identify.
- Click the “View” tab on Microsoft Word’s ribbon.
- Click the “Macros” button.
- Click the name of your macro to select it.
- Click the “Run” button. (If you wanted to delete the macro, you could press the “Delete” button instead.)
After the macro runs, a dialog box will appear with the numeric code for the character. To dismiss the dialog box, click OK.
Now that you know the code for the character after your cursor, you can use that code in Word’s Find dialog. Or you can insert it into your document. To do so, hold down the ALT key and enter the code on the numeric keypad. For example, the code for an uppercase A is 65. You could insert that character by holding down the ALT key and entering 65 on the numeric keypad. Of course, it’s a lot easier just to hold down SHIFT and hit the A key, but what if you need to enter something more esoteric? Microsoft provides a code chart here: ANSI Character Codes Chart.
That’s it! I hope you find the macro useful, and that my explanation here helps you understand a little more about how macros work.
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.
3 Comments