Solved

Shading Paragraphs using VBA

Posted on 2015-01-28
5
78 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

815 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