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

Removing Automatic Lines

Type a few dashes, underscores, or equal signs, and you could end up with a full-width line in your document. This is ...

Discover More

Tracing Errors

Sometimes it can be confusing to figure out the source of an error that is displayed in your worksheet. Excel provides a ...

Discover More

Using the FORECAST Function

Excel provides a handy worksheet function that allows you to forecast values based upon a set of known values. This ...

Discover More

Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!

More WordTips (ribbon)

Controlling the Italic Text Attribute

If you are formatting your document by using a macro, you may need to make some of your text italics. You do that by ...

Discover More

Adding a Macro to the Quick Access Toolbar

One of the easiest ways to quickly access a macro is to assign it to the Quick Access toolbar. Here's how you can make ...

Discover More

Bypassing the Startup Macro

Word allows you to create a macro that is run automatically whenever the program is started. If you want to bypass the ...

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 eight less than 8?

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.