Highlighting Every Other Line

Written by Allen Wyatt (last updated February 26, 2022)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021


1

Lorraine often writes speeches and to ease presentations she highlights every other line of her document in gray. This allows her to keep her place in the document when reading it. As there is no hard or soft return at the end of each line, Lorraine ends up adding the gray manually. She wonders if there is an easier way to add the highlighting.

There is an easier way, and it involves using a macro. It would be great if you could do the shading using styles, but such is not the case in Word. The following is an example of a macro that will mark every other line of the document in gray.

Sub HighlightLines()
    Dim iLines As Long
    Dim J As Integer

    If Selection.StoryType = wdMainTextStory Then
        Selection.WholeStory
        iLines = Selection.Range.ComputeStatistics(wdStatisticLines)
        Selection.Range.HighlightColorIndex = wdAuto
        Selection.Collapse

        For J = 1 To iLines Step 2
            Selection.GoTo What:=wdGoToLine, Which:=wdGoToAbsolute, Count:=J
            Selection.Expand Unit:=wdLine
            Selection.Range.HighlightColorIndex = wdGray25
        Next

        Selection.Collapse
        Selection.HomeKey Unit:=wdStory
    Else
        MsgBox "The insertion point is not within the main text."
    End If
End Sub

If your insertion point is not in the main text of the document (for instance, it is in a header or footer), then the macro won't do anything except inform you of the problem. Otherwise, it calculates how many lines are in the document and then uses a For...Next loop to step through each line.

Actually, the way the For...Next loop is initiated is rather important. The pertinent line looks like this:

        For J = 1 To iLines Step 2

There are two things you might want to change in this line. First, if your document has a title at the very beginning, such as the title for your speech, and you don't want it marked in gray, then you should change the 1 (the starting point of the loop) to 2. Second, if you want to shade your document's lines by some other increment, then change the Step value to the desired increment. For instance, if you want to shade every third line, you would change "Step 2" to "Step 3".

Note, as well, that the macro gets rid of any shading already in the document. This is helpful because you may have already shaded the document's lines, and then did some editing. Run the macro again, and the old shading is removed and new shading applied to the appropriate lines.

Finally, you should be aware that the macro essentially ignores tables, footnotes, endnotes, and any text in shapes or text boxes. They are not considered part of the main document, so the macro has no effect on them.

The macro does take a noticeable while to run, depending on the length of your document and the speed of your system. On my rather fast system with a 27-page document, it took about 6 seconds to complete. Granted, 27 pages would be a very long speech, so when I ran it on a shorter 6-page document, it took only about a second to run.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (10592) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Quick AutoFill Variations

The AutoFill feature can be used for more than just incrementing information into cells. This tip explains how to access ...

Discover More

Saving Documents as Read-Only by Default

When you save your documents, you can specify that they be saved in a "read-only" format so that they cannot be changed ...

Discover More

Highlighting Duplicate Words

One way to help improve your writing is to minimize the number of duplicated words you use in your prose. Depending on ...

Discover More

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!

More WordTips (ribbon)

Determining if Caps Lock is On

If your macro needs to determine the status of the Caps Lock key, you need the code in this tip. Just use the Information ...

Discover More

Bumping Numbers in a Document

If your documents include words that contain numbers (such as a list of parts numbers) you may need a way to increment ...

Discover More

Calculated Dates

Word makes it easy to insert today's date in a document, but not as easy to insert a date X number of days in the future. ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 2 + 2?

2022-02-26 23:33:51

Ken Endacott

The HighlightLines macro is very slow. On my medium speed PC the run time for a 31 page document was 41 seconds and for a 327 page document 2 hours 2 seconds. The process time increases at a rate greater than document size increase.

The speed can be greatly improved by using Range rather than Selection in the macro. The following macro takes 6.8 seconds on my machine for the 31 page and 78 seconds for the 327 page document.. The process time for this macro is proportional to the document size.

Sub HighlightLinesFastVersion()
Dim iLines As Long
Dim J As Integer
Dim aRange As Range
Dim bRange As Range
If Selection.StoryType = wdMainTextStory Then
Selection.WholeStory
iLines = Selection.Range.ComputeStatistics(wdStatisticLines)
Selection.Range.HighlightColorIndex = wdAuto
Selection.Collapse
J = 1
Set aRange = Selection.Range
Do
Set aRange = aRange.GoTo(What:=wdGoToLine, Which:=wdGoToNext, Count:=2)
Set bRange = aRange.GoTo(What:=wdGoToLine, Which:=wdGoToNext, Count:=1)
aRange.End = bRange.Start
aRange.HighlightColorIndex = wdGray25
J = J + 2
Loop While J < iLines
Selection.Collapse
Selection.HomeKey unit:=wdStory
Else
MsgBox "The insertion point is not within the main text."
End If
End Sub


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.