Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Repeat Action to End of a Document-Word 2007 VBA

Status
Not open for further replies.

ITBeginnerKT

Technical User
Oct 20, 2012
1
US
Hi,

I am very new to the IT world. I am working with creating a macro using VBA in Word 2007. The objective of the macro is to highlight every 3rd line in yellow all the way through the document whether it is 10 lines, 10 paragraphs, or 10 pages long. I am including the information that I have recorded on the macro so far which has allowed it to highlight the 1st line and skip down 2 lines to get to the 3rd line. I am looking for the information that needs to go above and below the instructions that will allow it to perform the action until the end of a document.

I believe it is a "Do..Loop Until <condition>" type statement but I do not know what the condition is after until to be able to make it work. I am guessing that this is a very entry level type programming question but I would appreciate any assistance! Thank you for your help!

Do

Selection.HomeKey Unit:=wdStory
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Options.DefaultHighlightColorIndex = wdYellow
Selection.Range.HighlightColorIndex = wdYellow
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=2

Loop Until <condition>
 
If you're willing to use the "paragraph" object rather than the line, you could use the collection:
Code:
Sub hlines()
    lcount = 0
    For Each p In ThisDocument.Paragraphs
        lcount = lcount + 1
        If lcount Mod 3 = 0 Then
            With p.Shading
                .Texture = wdTexture12Pt5Percent
                .BackgroundPatternColorIndex = wdYellow
                .ForegroundPatternColorIndex = wdBlack
            End With
        End If
    Next
End Sub

But that's probably not what you want. You can capture the return value of "Selection.MoveDown Unit:=wdLine, Count:=2" like
Code:
rtncount=Selection.MoveDown Unit:=wdLine, Count:=2
then you stop when rtncount<>2

_________________
Bob Rashkin
 
Trying to work with lines in Word is quite problematic.

For starters, as soon as you change printers and/or printer drivers, the layout is liable to change and what was previously on one line may no be on another. And, if you've got tables, trying to keep the 'lines' in synch across multiple columns can be a nightmare - especially once you start mixing formats & cell alignments and throw in the odd merged/split cell.

Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top