Solved

Shading Paragraphs using VBA

Posted on 2015-01-28
5
74 Views
Last Modified: 2015-01-30
Dear Experts:

for some specific reason I would like to shade every other 'product' (the list contains hundreds of these entries) as you can see in the screen shot using a macro

Help is very much appreciated, thank you very much in advance.

Regards, Andreas

apply grey shadingShading-every-other-product.docx
0
Comment
Question by:AndreasHermle
  • 3
  • 2
5 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40576311
Hi Andreas.
If you use Shading, it extends between paragraphs. Highlighting would look more like your picture.
Here are macros for each:
Sub ShadeIt()
    Dim para As Paragraph
    Dim p As Integer
    
    For Each para In ActiveDocument.Range.Paragraphs
        If p Mod 4 > 1 Then
            para.Range.Shading.BackgroundPatternColor = wdColorGray15
        End If
        p = p + 1
    Next para
End Sub

Open in new window

Sub HighLightIt()
    Dim para As Paragraph
    Dim p As Integer
    
    For Each para In ActiveDocument.Range.Paragraphs
        If p Mod 4 > 1 Then
            para.Range.HighlightColorIndex = wdGray25
        End If
        p = p + 1
    Next para
End Sub

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40576315
I didn't see that you had posted a sample document, but the answer still stands.
0
 

Author Comment

by:AndreasHermle
ID: 40577083
Hi Graham,

sorry for the delay in giving a feedback. Thank you very much for your professional efforts, as always the macro runs nicely.

However there is one thing I would like to get changed:

As you can see in the screenshot, not the whole paragraph should be shaded but just the text and moreover ...
... I would like to run this macro on a selection.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 40578585
Hi Andreas, as I hinted in my answer, "Shading" in Word technology refers to a Range, so that it will colour the whole of the range and not just the individual characters. Highlighting, however,  does target individual characters, so, for working on the selection only,  I suggest a slight variation on my second suggestion
Sub HighLightIt()
    Dim para As Paragraph
    Dim p As Integer
    
    For Each para In Selection.Paragraphs
        If p Mod 4 > 1 Then
            para.Range.HighlightColorIndex = wdGray25
        End If
        p = p + 1
    Next para
End Sub

Open in new window

0
 

Author Closing Comment

by:AndreasHermle
ID: 40579954
Hi Graham, as always, thank you very much for your professional and swift support. Works as desired, and as always, great and detailed explanation.

Regards, Andreas
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now