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
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.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
When you use a computer at work, that computer may be subject to periodic updating or replacement. If you want to protect ...
Discover MoreYou can use a macro to print to any printer you have defined in Windows. It is good practice, if you are changing which ...
Discover MoreWant your macro to change the Hidden attribute for some text in your document? It's easier to change than you might think.
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
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
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.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments