An American Editor

March 31, 2014

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.

8 Comments »

  1. This macro elicits a “syntax error” and won’t run.

    Like

    Comment by C.A.Johnston — September 5, 2014 @ 1:26 pm | Reply

    • I believe the syntax error is caused by the conversion of the macro by the WordPress blog, specifically the quotation marks in these two lines:

      .Text = “^p^p”
      .Replacement.Text = “”

      If you’ll change those curly quotation marks to plain, straight ones, with no spaces around them, the macro should run correctly.

      Like

      Comment by Jack Lyon — September 5, 2014 @ 2:07 pm | Reply

      • And, actually, we have the same problem on the two commented lines:

        ‘To clean returns in endnotes rather than footnotes, change “.Footnotes” to “.Endnotes” in the following line:

        ‘The following line may trigger an error!

        If you’ll change the curly quotation marks to straight ones, all should be well.

        Like

        Comment by Jack Lyon — September 5, 2014 @ 2:09 pm | Reply

  2. I moved the Endnotes to a position before the Works Cited, by creating two section braks after the document text and the Works Cited, and copying them between those two breaks. Then I dleted as much of the orginal endnotes (after the Works Cited), but was left with some carriage returns and a separator line at the top. I ran this macro successfully, but of course it only ran through the Endnote section ABOVE the Works Cited. How can I get rid of all that junk BELOW the Works Cited? Thanks for your help; very well-written.

    Like

    Comment by Robert Kafka — December 15, 2014 @ 2:57 pm | Reply

    • Hi, Robert.

      It’s hard for me to answer your question without actually seeing your document. If you’ll send it to me, I’ll be glad to take a look.

      Best wishes,
      Jack Lyon
      The Editorium

      Like

      Comment by Jack Lyon — December 17, 2014 @ 5:46 pm | Reply

  3. Great macro (after applying the revisions listed in the comments). I’d suggest adding a comment to the macro that includes instructions for getting into the footnotes in draft mode, as this will be easily forgotten if the macro isn’t used often.

    Like

    Comment by michaeleschuler — June 1, 2015 @ 1:00 pm | Reply

  4. Here’s a macro I’ve been working on, case anyone finds it helpful. It cleans up your endnotes (or footnotes, with a few easy adjustments). No errors to trap, at least so far!

    Sub EndnotesCleanup()

    If ActiveDocument.Range.Endnotes.Count > 0 Then
    Dim Graf As Paragraph

    ‘Multiple spaces = 1 space
    Set Rng = ActiveDocument.StoryRanges(wdEndnotesStory)
    With Rng.Find
    .Text = ” {2,}”
    .Replacement.Text = ” ”
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
    End With

    ‘Cut leading and trailing spaces from paragraphs
    Set Rng = ActiveDocument.StoryRanges(wdEndnotesStory).Paragraphs
    For Each Graf In Rng
    If Graf.Range.Characters.First = ” ” Then _
    Graf.Range.Characters.First = “”
    If Graf.Range.Characters.Last.Previous = ” ” Then _
    Graf.Range.Characters.Last.Previous = “”
    Next

    ‘Delete empty paragraphs
    Set Rng = ActiveDocument.StoryRanges(wdEndnotesStory)
    With Rng.Find
    .Text = “^13{2,}”
    .MatchWildcards = True
    While .Execute
    Rng.MoveEnd wdCharacter, -1
    Rng.Delete
    Wend
    End With

    ‘Add period at end of each enote if it’s missing (and there’s no _
    ?, !, or closing quotes)
    Set Rng = ActiveDocument.StoryRanges(wdEndnotesStory).Paragraphs
    For Each Graf In Rng
    Set ChrL = Graf.Range.Characters.Last.Previous
    If (ChrL “?”) And _
    (ChrL “!”) And _
    (ChrL Chr(148)) And _
    (ChrL “.”) Then
    ChrL.InsertAfter “.”
    End If
    Next

    End If
    End Sub

    Like

    Comment by Pablo Morales — March 2, 2018 @ 5:39 pm | Reply

    • Note from AAE: This macro has not been tested by AAE or by any of the regular essayists on AAE. If you try the macro, be sure to do so on a test document first.

      Like

      Comment by americaneditor — March 3, 2018 @ 3:18 am | 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.

Blog at WordPress.com.