Lyonizing Word: Let’s Go Spelunking

 Let’s Go Spelunking

by Jack Lyon

Spelunking is the recreational pastime of exploring caves. It’s a dark and dangerous hobby, an extreme sport for those who are confident in their ability to climb, navigate, and even swim (there’s usually water down there).

I try to avoid such hazards, but I’m not afraid to explore some of the deeper reaches of a computer program — Microsoft Word, for example. That’s one reason I know quite a bit about that particular program. Some of my friends, however, seem terrified of making a “mistake” on the computer. They want a concrete series of steps to follow in everything they do. “How can I make a word bold?” they ask. I reply:

  1. Double-click the word to select it.
  2. Click the “Bold” icon on the Ribbon.

Then they say, “Oh, that’s wonderful! Let me write that down for next time.”

There’s nothing inherently wrong with learning to use a computer in that way, and those who are comfortable with that should keep a big Microsoft Word reference book close at hand. These are probably the same people who would enjoy taking a guided tour of Timpanogos Cave, which is about an hour away from where I live.

But that’s a far cry from spelunking, and I doubt that any of the people on the tour discover something new.

So what kind of a person are you? Do you like someone to hold your hand along the well-marked trail? Or would you rather descend into the dark depths of the cavern with only a flashlight as your guide? Either way is fine, but sometimes it’s nice to get off the beaten path; you never know what you might find. As Henry David Thoreau once said, “Nature abhors a vacuum, and if I can only walk with sufficient carelessness I am sure to be filled.”

Want to learn something new about Word? Try exploring Word’s features that aren’t on any menu, the caverns that aren’t on the map. Here’s how:

  1. Press ALT+F8 to open the Macros dialog.
  2. Click the dropdown list next to “Macros in.”
  3. Select “Word Commands.”

Now, in the window under “Macro name,” you’ll see all of the commands available in Microsoft Word, whether they’re on the Ribbon or not. If you click one, you’ll see a description of its function under “Description,” at the bottom of the dialog. These descriptions are minimal at best, but along with the name of the command, they’ll give you some idea of what the command does. You can also click the “Run” button to run the command, which may give you even more insight. (Be sure to do this only with a junk document; you don’t want mess up an actual project.)

Let’s take a look. Don’t be afraid; I’ll be right behind you all the way.

So we’re scrolling through the list of Word commands in Word 2013, and what do we see? “CharacterRemoveStyle,” which, according to its description, “Clears character style from selection.” What?!? Does this mean it’s possible to remove a character style without affecting text-level formatting (such as italic)? If so, I sure didn’t know about it. Let’s find out. We type a junk sentence into a junk document:

This is a test to see what will happen.

We apply italic formatting to “test” and the character style “Emphasis” character style to “see”:

This is a test to see what will happen.

The formatting of those two words looks the same, but the formatting is not the same. Now let’s see if the “CharacterRemoveStyle” command works. We select the sentence, press ALT+F8, scroll down to “CharacterRemoveStyle,” and run it. Look at that! Our test sentence becomes:

This is a test to see what will happen.

The character style is gone, but the text-level formatting is still there. Neat!

Okay, one more, and then we’ll go back up to the surface. Down, down, down, scrolling, scrolling, scrolling. What’s this? “RestoreCharacterStyle.” I’ve never noticed that command before. The description says “Restores character style and removes direct formatting.” Could this be the inverse of the command we just finished exploring? Again we type our junk sentence and apply the same formatting as before:

This is a test to see what will happen.

Then we select the sentence and run the “RestoreCharacterStyle” command. Yes! The sentence now looks like this:

This is a test to see what will happen.

The text-level formatting is gone, but the character style remains!

But why does Microsoft say that this command restores a character style? If we remove the character style from our sentence and then run the command, does the character style come back? A quick experiment shows us that no, it doesn’t. Then why the odd name? I suspect that under the hood, Word is removing all character-level formatting but then restoring any formatting applied with a character style. It’s the equivalent of (1) identifying the character style, (2) pressing CTRL+SPACEBAR (to remove character-level formatting), and then (3) reapplying the character style — which means that the command was named from the programmer’s perspective rather than the user’s perspective. There’s a lot of stuff like that down here in the dark, and it’s part of what makes exploring so interesting.

Back up in the daylight, we assess our adventure, which I’d have to say has been a success. We’ve discovered two commands we didn’t know about before. Could they be useful in our actual editing work? Yes, indeed!

Personally, I enjoy crawling around down there in the bowels of Microsoft Word. Yes, it’s dark and it’s dirty, and sometimes I find something nasty under a rock. But I also make lots of interesting discoveries, and I nearly always learn something new.

How about you? Ready to go spelunking on your own? Have fun, and don’t forget your flashlight!

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.

Lyonizing Word: Deleting Extraneous Carriage Returns in Footnotes and Endnotes

Deleting Extraneous Carriage Returns
in Footnotes and Endnotes

by Jack Lyon

During my editing career, I've often run into problems with footnotes and endnotes in Microsoft Word. Many authors have no clue about how notes are meant to work, and Microsoft certainly hasn't made it obvious. In fact, they've made it easy to mess notes up beyond repair.

One mistake authors make is to insert extraneous carriage returns before or after a note. Why? Because they don't like the positioning of the note on the page, which they're trying to make "pretty," not understanding the problems that will cause in editing and typesetting.

You can try to remove the extraneous returns like this:

1. Click View > Draft.
2. Click References > Show Notes.

Your cursor should now be in the Notes pane.

3. Click Home > Replace.
4. In the "Find What" box, enter two paragraph codes:

^p^p

5. In the "Replace With" box, enter one paragraph code:

^p

 6. Click the "Replace All" button.

Word will replace some of the double returns, but not all of them. And if you try to delete some of the remaining returns, you'll get an error message:

"This is not a valid action for footnotes."

What's going on there is that not all carriage returns are created equal. Some of the returns are "special" note returns, and the only way to delete them is to delete the note itself back in the text.

The solution? A macro, of course. But a macro with a twist. As we've seen, the macro can't just find double returns and replace them with a single return. And trying to delete extra returns results in an error. So let's use that error!

Before running the macro, you must be in Draft view, with your cursor at the top of the Notes pane. (How to get there is explained above.)

In the macro, you'll see a couple of "comments," which are explanations or instructions intended for the person reading the code. Comments are preceded by a single quotation mark ('), which tells the macro to ignore the rest of the text on that line. For example, the first comment in the macro reads:

'To clean returns in endnotes rather than footnotes, change ".Footnotes" to ".Endnotes" in the following line:

And now, here's the macro:

Sub CleanReturnsInNotes()
'To clean returns in endnotes rather than footnotes, change ".Footnotes" to ".Endnotes" in the following line:
NoteCount = ActiveDocument.Footnotes.Count
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find

.Text = "^p^p"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False

End With
Selection.Find.Execute
On Error GoTo TrapTheError
While Selection.Find.Found

Selection.MoveLeft
'The following line may trigger an error!
Selection.Delete
Selection.Find.Execute

Wend
GoTo TheEnd
TrapTheError:

ErrorCount = ErrorCount + 1
Selection.MoveRight
Selection.Delete
If ErrorCount < NoteCount Then Resume Next

TheEnd:
End Sub

Let's look at some of those lines.

NoteCount = ActiveDocument.Footnotes.Count

NoteCount is a variable; that is, it's a container that can hold a numerical value—in this case, the number of footnotes in the document. We get that value with the VBA command ActiveDocument.Footnotes.Count.

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting

Just to be safe, these lines clear any formatting that might already be applied to the "Find What" and "Replace With" boxes in Word's find and replace feature.

The following lines, from

With Selection.Find

down to

Selection.Find.Execute

simply find any instances of double paragraph returns. The replacement text is set to nothing, as we're not trying to replace those returns with anything:

 .Replacement.Text = ""

Instead, we're going to try to delete the second return, which (unless the notes are really messed up) is a regular return rather than a special note return:

 Selection.MoveRight
Selection.Delete

If it's a special note return, then trying to delete it will cause an error, and the macro will execute this line—

On Error GoTo TrapTheError

—which sends the macro to this line:

TrapTheError:

Here's what happens next:

ErrorCount = ErrorCount + 1

Using the variable ErrorCount, we count the number of errors, adding 1 each time we find one. (ErrorCount is initially empty, or zero.)

Selection.MoveRight
Selection.Delete

We move right and delete the next return.

 If ErrorCount < NoteCount Then Resume Next

If the number of errors is less than the number of notes, we're not through yet, as one of the remaining notes may still have a bad return next to it. So, we tell the macro to Resume operation at the next command after the error occurred. That command is:

Selection.Find.Execute

In other words, Word looks for the next occurrence of a double return. And this construction—

While Selection.Find.Found

Selection.MoveLeft
'The following line may trigger an error!
Selection.Delete
Selection.Find.Execute

Wend

—ensures that it will keep looking as long as (While) double returns are found. ("Wend" is short for "While End"—it marks the end of the While construction.)

GoTo TheEnd

When no more double returns are found, this line is executed. It exists simply to avoid executing the error trap (TrapTheError and the following lines) after the macro is finished, at which point

TheEnd:

marks the end of the whole operation.

I hope this explanation has helped you understand better how macros work, and in particular how you can actually use Word errors to force Word to do what you want it to do—something that gives me great pleasure.

Even if you don't understand everything that's going on in this macro, you can still use it to clean up extraneous returns in notes—something that should make your editorial life a little bit easier.

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.