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

Two Keys with the Press of One

Sometimes it could be helpful to have Word substitute two characters for the one that you type, for instance, to replace ...

Discover More

Reversing a String

Need to reverse all the characters in a string? You can do so by using the function described in this tip.

Discover More

Turning Off Smart Quotes for Specific Styles

Smart quotes can be helpful in making a great-looking document, but at times, they can be a real pain. Wouldn’t it be ...

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 a Random Value

If you need to determine a random value in a macro, you can do so using the Rnd function. This tip presents the syntax ...

Discover More

Determining the Week of the Year

If you are working with dates in a macro, you may need to determine which week of the year a date falls within. This can ...

Discover More

Inserting a Formatted Text Box with a Macro

Macros allow you to do just about anything in Word, but not if you limit yourself to using just the Macro Recorder. This ...

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 6 - 0?

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.