?
Solved

How to Select Every Other Line in a Word Document Using VBscript

Posted on 2013-01-05
9
Medium Priority
?
2,781 Views
Last Modified: 2013-01-05
Hello Experts.
I've been trying to create a simple macro that goes through a small word document and select all text on every other line. I have hundreds of documents where I have to do this manually, and it would really help if I could get a macro to do it.
It's definitely not correct, but here's the script I have right now:
Sub HighlightAlternateLines()
  Dim par As Paragraph
  For Each par In ActiveDocument.Paragraphs
    Selection.ExtendMode = True
    Selection.Expand wdParagraph
    GoDown = Selection.MoveDown(Unit:=wdLine, Count:=2)
  Next
End Sub

Open in new window

Any assistance you can offer would be fabulous.

Best,
Tony
0
Comment
Question by:tonygardner1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 38746575
Not clear what you expect to achieve. This will do it, but when it stops only the last odd paragraph is selected, since the previous odd paragraphs are not contiguous.
Sub SelectAlternateParagraphs()
    Dim par As Paragraph
    Dim Odd As Boolean
    
    For Each par In ActiveDocument.Paragraphs
        Odd = Not Odd
        If Odd Then
            par.Range.Select
        Next
    Next par
End Sub

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 38746587
There is only one Selection object in a Word application with a single Start and a single End property, so making a selection automatically deselects what was previously selected.
0
 
LVL 8

Expert Comment

by:stevepcguy
ID: 38746637
Could you clarify what you want to achieve with this? As already stated, you can have only one object selected at a time, unlike Excel, where you can have multiple groups of cells selected at once.

Is this to make every other line shaded a particular color? Make it bold, perhaps? That could be done using the For/next loop, but we would need to know what you plan to do.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:tonygardner1
ID: 38747474
Thanks Graham and Steve for the helpful responses. I guess I was hoping to find a VBscript equivalent to do what I'm doing manually which is holding the Mac Command button (Ctrl in Windows) and clicking to the left of every other line. Once the alternate lines are selected, I set their color to wdColorDarkBlue. The reason I wanted to have the selection process be a separate macro was in case the lines whose text needs to be dark blue are not perfectly setup. For example, there might be some preliminary text that is inserted.

The perfect VBscript to accomplish my objective would need to go through the words on the line to see if they meet a certain percentage of pre-defined criteria, and if so, set the entire line to dark blue and move on to the next. For simplicity sake, let's say that the criteria is that the first letter of all words is [A-G], and the percentage must be greater than or equal to 90%.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 38747567
It sounds as if you need something like this:
Sub RecolourSomeParagraphs()
    Dim par As Paragraph
    Dim Odd As Boolean
    Dim wrd As Range
    Dim FirstLetterCount As Integer
    
    For Each par In ActiveDocument.Paragraphs
        Odd = Not Odd
        If Odd Then
            Debug.Print par.Range.Text
            FirstLetterCount = 0
            For Each wrd In par.Range.Words
                Select Case wrd.Characters.First
                    Case "A" To "G", "a" To "g"
                        FirstLetterCount = FirstLetterCount + 1
                End Select
            Next wrd
            Debug.Print FirstLetterCount, FirstLetterCount / par.Range.Words.Count
            If FirstLetterCount / par.Range.Words.Count >= 0.9 Then
                par.Range.Font.Color = wdColorDarkBlue
            End If
        End If
    Next par
End Sub

Open in new window


Incidentally, this is VBA, not VBScript.
0
 

Author Comment

by:tonygardner1
ID: 38747685
Wow, Graham. This looks really promising. I tried it on my test document, but the lines that should have met our criteria didn't get set to dark blue.

Also note that we no longer need to worry about alternate lines since we're now checking every paragraph. I did try commenting out the lines referencing Odd/Even, but that didn't seem to help.

I have attached a sample document where the objective would be to have all lines containing guitar chords set to dark blue.

Thanks Again,
Tony G.
The-Quick-Brown-Fox.docx
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 38747713
The chord lines have many spaces, so this code simply looks for paragraphs containing 10 or more adjacent spaces.
Sub RecolourChordParagraphs()
    Dim par As Paragraph
    Dim wrd As Range
    Dim FirstLetterCount As Integer
    
    For Each par In ActiveDocument.Paragraphs
        If InStr(par.Range.Text, String(10, " ")) Then
            par.Range.Font.Color = wdColorDarkBlue
        End If
    Next par
End Sub

Open in new window

0
 

Author Comment

by:tonygardner1
ID: 38748073
Thanks ever so much, Graham. That's really good thinking. I really appreciate your help.
0
 

Author Closing Comment

by:tonygardner1
ID: 38748074
Excellent work!
0

Featured Post

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question