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.

Lyonizing Word: The Next Character Macro

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:

  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 “NextCharacter.”
  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 directly in front of the character you want to identify.
  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.)

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.