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

Saving in PostScript Format

Word can save your document in PostScript format so that it can be easily processed by other programs that work with ...

Discover More

Finding Other Instances of Excel in a Macro

When processing information using a macro, you may need to know if there are any other instances of Excel running on a ...

Discover More

Starting in Safe Mode

By using a command-line switch, Excel can be started in safe mode. This means that the program is loaded with bare-bones ...

Discover More

Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!

More WordTips (ribbon)

Converting Words into Numbers

Sometimes you need to spell-out numbers in a document. When you have spelled-out numbers, at some point you might want to ...

Discover More

Saving Changes when Closing

If you write a macro that makes changes to a document, you may want that macro to save those changes. There are several ...

Discover More

Inserting the Time Remaining Until a Target Date and Time

Would you like a countdown value of some type to appear in your document? You can create your own through the use of a ...

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 two less than 9?

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.